注册页面php验证用户名是否存在。

reg.php

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  6 <link rel="stylesheet" type="text/css" href="css/int.css" />
  7 <script type="text/javascript" src="js/func.js"></script>
  8 <style type="text/css">
  9 td{
 10     height:30px;
 11     vertical-align:middle;
 12     align:center;
 13 }
 14 #myText{
 15  width:600px;
 16 }
 17 </style>
 18 <title>注册页面</title>
 19 </head>
 20 
 21 <body >
 22 
 23 <?php
 24 error_reporting(0);
 25 //不让PHP报告有错语发生。如果不关闭好有类似这的错语 Warning: preg_match() 关闭就不出现了
 26 session_start();
 27 header("Cache-control: private");
 28 
 29 $conn = @ mysql_connect("localhost","root","")or die("数据库连接错误");
 30 mysql_select_db("bbs",$conn);
 31 mysql_query("set names utf8");
 32 
 33 if($_POST['submit'])
 34 {   
 35     $username = $_POST["username"];
 36     
 37     $sql="select userName  from user_info where userName='$username'";
 38  //   echo $sql;
 39     
 40     $query=mysql_query($sql);
 41     $rows = mysql_num_rows($query);
 42     if($rows > 0){
 43          echo "<script type='text/javascript'>alert('用户名已存在');location='javascript:history.back()';</script>";
 44     }else{
 45         $user_in = "insert into user_info (username,pass,sex,qq,email,img) values ('$_POST[username]',md5('$_POST[pass]'),'$_POST[sex]','$_POST[qq]','$_POST[email]','$_POST[img_select]')";        
 46         //echo $user_in;
 47         mysql_query($user_in);
 48         echo "<script type='text/javascript'>alert('写入成功!!');location.href='login.php';</script>";
 49        
 50     }
 51 
 52 //javascript:history.go(-1)
 53     
 54 }
 55 ?>
 56 
 57 
 58 <form  action="reg.php" name="reg_form" method="post" onsubmit="return check_reg()">
 59 <table name="reg_table" align="left">
 60 <tr>
 61     <td>用户:</td><td><input id="username" name="username" class="myText" type="text" maxlength="12" /></td>
 62 </tr>
 63 
 64 <tr> <!--性别:0 保密 1 女 2 男-->
 65      <td > 性别:</td>
 66      <td>女<input type="radio" value="1" name="sex"/>
 67          男<input type="radio" value="2" name="sex" />
 68        保密<input type="radio" value="0" name="sex"  checked/></td>
 69 </tr>
 70 
 71 <tr>
 72     <td>密码:</td><td><input name="pass" class="myText" type="password" onblur="check_len(this)"/><span id="show_pass" style="color:red;"></span></td>
 73 </tr>
 74 
 75 <tr>
 76     <td>重复密码:</td><td><input name="repass" class="myText" type="password" onblur="check_pass(this)" /><span id="show_repass" style="color:red;"></span></td>
 77 </tr>
 78 
 79 <tr>
 80     <td>QQ:</td><td><input type="text" class="myText"  name="qq" onblur="check_qq(this)"/><span style="color:red;" id="show_qq"></span></td>
 81 </tr>
 82 
 83 <tr>
 84     <td>邮箱:</td><td><input type="text" class="myText" name="email"  onblur="check_email(this)"/><span id="show_e" style="color:red;"></span></td>
 85 </tr>
 86 
 87 <tr>
 88     <td height="60">头像:</td>
 89     <td>
 90     <select name="img_select" onchange="img_change(this)">
 91         <option value="101" >女 001</option>
 92         <option value="102" >女 002</option>
 93         <option value="103" >女 003</option>
 94         <option value="104" >女 004</option>
 95         <option value="105" >男 001</option>
 96         <option value="106" >男 002</option>
 97         <option value="107" >男 003</option>
 98         <option value="108" >男 004</option>
 99      </select>
100      <img src="/bbs/img/101.gif" id="tx_change" style="width:50px; height:65px;" alt=""/>
101     </td>
102 </tr>
103 
104 <tr height="20" align="justify">
105     <td align="right" ><input type="submit" value="注册" name="submit" style="margin-right:5px;"/></td>
106     <td><input type="reset"  value="重置" name="reset" style="margin-left:5px;"/></td>
107 </tr>
108 
109 <tr>
110     <td  colspan="2">我已有账号现在<a href="login.php">登录</a></td>
111 </tr>
112 
113 </table>
114 </form>
115 </body>
116 </html>

 

 

func.js

  1 //根据下拉框变换图片
  2 function img_change(thisObj){
  3     var imgsrc = "/bbs/img/"+ thisObj.value+".gif";
  4     document.getElementById("tx_change").src=imgsrc;    
  5 }
  6 
  7 //检查是否都符合 注册 要求
  8 function check_reg()
  9 {
 10 if(check_len() && check_pass() && check_email() && check_qq())
 11 {
 12     return true;    
 13 }else{
 14     return false;
 15     }
 16 }
 17 
 18 //检查密码长度不能少于6
 19 function check_len(thisObj){
 20     if(thisObj.value.length==0)
 21     {
 22         document.getElementById('show_pass').innerHTML="密码不能为空";
 23         return false;
 24     }else{
 25     if (thisObj.value.length<6)
 26     {
 27         document.getElementById('show_pass').innerHTML="密码长度不少于6";
 28         return false;
 29     }
 30         document.getElementById('show_pass').innerHTML="";    
 31         return true;
 32       }
 33 }
 34 
 35 //检查俩次密码输入是否一致
 36 function check_pass(thisObj){
 37     var psw=document.getElementById('pass');
 38     if(psw.value.length==0)
 39     {
 40         document.getElementById('show_pass').innerHTML="密码不能为空";
 41         return false;
 42     }else{
 43         document.getElementById('show_pass').innerHTML="";    
 44 
 45     if (thisObj.value!=psw.value)
 46     {
 47         document.getElementById('show_repass').innerHTML="两次密码输入不正确";
 48         return false;
 49     }
 50         document.getElementById('show_repass').innerHTML="";
 51         return true;
 52     }
 53 }
 54 
 55 //检查email是否正确
 56 function check_email(thisObj){
 57     var reg=/^([a-zA-Z\d][a-zA-Z0-9_]+@[a-zA-Z\d]+(\.[a-zA-Z\d]+)+)$/gi;    
 58     var rzt=thisObj.value.match(reg);
 59     if(thisObj.value.length==0){
 60         document.getElementById('show_e').innerHTML="Email不能为空";
 61         return false;
 62         }else{
 63     if (rzt==null)
 64     {
 65         document.getElementById('show_e').innerHTML="Email地址不正确";
 66         return false;
 67     }
 68         document.getElementById('show_e').innerHTML="";
 69         return true;
 70   }
 71 
 72 }
 73 
 74 //检查qq格式是否正确
 75 function check_qq(thisObj){
 76     var qq=document.getElementById('qq').value;
 77     var reg=/^\d+$/;
 78     if(qq.search(reg))
 79     {
 80         document.getElementById('show_qq').innerHTML=" QQ 只能为数字";
 81         return false;
 82     }else{
 83         document.getElementById('show_qq').innerHTML="";
 84         return true ;
 85         }
 86     
 87     
 88 }

个人原创 转载请注明 。

 

posted @ 2012-07-13 11:49  Lucky樱桃  阅读(10823)  评论(1编辑  收藏  举报
View Code