验证用户注册信息是否合理
1、通过JavaScript脚本技术来实现判断用户输入的注册信息是否合理
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 <title>验证用户注册信息是否合理</title> 6 <style type="text/css"> 7 <!-- 8 body,td,th { 9 font-size: 12px; 10 } 11 --> 12 </style></head> 13 <script language="javascript"> 14 function checkemail(email){ 15 var str=email; 16 //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号 17 var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //定义email地址格式 18 var objExp=new RegExp(Expression); 19 if(objExp.test(str)==true){ 20 return true; 21 }else{ 22 return false; 23 } 24 } 25 function checktel(tel){ 26 var str=tel; 27 //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号 28 var Expression=/(\d{3}-)(\d{8})$|(\d{4}-)(\d{7})$/; //定义电话格式 29 var objExp=new RegExp(Expression); 30 if(objExp.test(str)==true){ 31 return true; 32 }else{ 33 return false; 34 } 35 } 36 function checkit(){ //自定义函数 37 if(form1.name.value==""){ //判断用户名是否为空 38 alert("请输入用户名!"); 39 form1.name.select(); 40 return false; 41 } 42 if(form1.pwd.value==""){ //判断密码是否为空 43 alert("请输入密码!"); 44 form1.pwd.select(); 45 return false; 46 } 47 if(form1.qpwd.value==""){ //判断确认密码是否为空 48 alert("请输入确认密码!"); 49 form1.qpwd.select(); 50 return false; 51 } 52 if(form1.pwd.value!=form1.qpwd.value){ //判断两次输入的密码是否一致 53 alert("两次输入的密码不一致!"); 54 form1.pwd.select(); 55 return false ; 56 } 57 if(form1.email.value==""){ //判断email是否为空 58 alert("请输入email!"); 59 form1.email.select(); 60 return false; 61 } 62 if(!checkemail(form1.email.value)){ //判断email地址格式是否正确 63 alert("您输入Email地址不正确!"); 64 form1.email.select(); 65 return false; 66 } 67 if(form1.tel.value==""){ //判断电话是否为空 68 alert("请输入电话!"); 69 form1.tel.select(); 70 return false; 71 } 72 if(!checktel(form1.tel.value)){ //判断电话号码格式是否正确 73 alert("您输入的电话不正确!"); 74 form1.tel.select(); 75 return false; 76 } 77 78 return true; 79 80 } 81 </script> 82 83 <body> 84 <form id="form1" name="form1" method="post" action=""> 85 <table id="__01" width="546" height="573" border="0" cellpadding="0" cellspacing="0"> 86 <tr> 87 <td colspan="3"><img src="images/zc_01.gif" width="546" height="121" alt="" /></td> 88 </tr> 89 <tr> 90 <td rowspan="2"><img src="images/zc_02.gif" width="24" height="452" alt="" /></td> 91 <td width="504" height="358" align="center"><table width="319" height="281"> 92 <tr> 93 <td width="86">用户名:</td> 94 <td width="221"><label> 95 <input type="text" name="name" /> 96 </label></td> 97 </tr> 98 <tr> 99 <td>密码:</td> 100 <td><input type="password" name="pwd" /></td> 101 </tr> 102 <tr> 103 <td>确认密码:</td> 104 <td><input type="password" name="qpwd" /></td> 105 </tr> 106 <tr> 107 <td>性别:</td> 108 <td><label> 109 <input name="sex" type="radio" value="男" checked="checked" /> 110 男</label> 111 <label> 112 <input type="radio" name="sex" value="女" /> 113 女</label></td> 114 </tr> 115 <tr> 116 <td>email:</td> 117 <td><input type="text" name="email" /></td> 118 </tr> 119 <tr> 120 <td>电话:</td> 121 <td><input type="text" name="tel" /></td> 122 </tr> 123 124 <tr> 125 <td colspan="2" align="center"><label> 126 <input type="submit" name="Submit" value="注册" onclick="return checkit();" /> 127 </label> 128 <label> 129 <input type="submit" name="Submit2" value="取消" /> 130 </label></td> 131 </tr> 132 </table></td> 133 <td rowspan="2"><img src="images/zc_04.gif" width="18" height="452" alt="" /></td> 134 </tr> 135 <tr> 136 <td><img src="images/zc_05.gif" width="504" height="94" alt="" /></td> 137 </tr> 138 </table> 139 </form> 140 </body> 141 </html>
2、更为简单的通过PHP语言及$_POST[]方法实现
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 <title>验证用户注册信息是否合理</title> 6 </head> 7 8 <body> 9 <?php 10 //连接数据库 11 $conn=mysql_connect("localhost","root","111"); 12 mysql_select_db("db_database04",$conn); 13 mysql_query("set names utf8"); 14 $name=$_POST['name']; 15 $pwd=$_POST['pwd']; 16 $qpwd=$_POST['qpwd']; 17 $sex=$_POST['sex']; 18 $mail=$_POST['email']; 19 $tel=$_POST['tel']; 20 if(isset($_POST['Submit']) and $_POST['Submit']=="注册"){ //判断用户是否单击注册按钮 21 if($name!=null){ //判断用户名是否为空 22 if($pwd==$qpwd){ //判断两次密码是否一致 23 if(preg_match("/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/",$email,$counts)){ //判断email地址 24 $insert=mysql_query("insert into tb_user(name,pwd,qpwd,sex,email,tel) values('$name','$pwd','$qpwd','$sex','$mail','$tel')",$conn); 25 if($insert){ 26 echo "<script> alert('注册成功!'); window.location.href='index.php'</script>"; 27 }else{ 28 echo "<script> alert('注册失败!'); window.location.href='index.php'</script>"; 29 } 30 }else{ 31 echo "<script> alert('email地址不正确!'); window.location.href='index.php'</script>"; 32 } 33 }else{ 34 echo "<script> alert('密码不一致!'); window.location.href='index.php'</script>"; 35 } 36 }else{ 37 echo "<script> alert('请输入用户名!'); window.location.href='index.php'</script>"; 38 } 39 } 40 ?> 41 42 </body> 43 </html>