验证表单

来源:互联网 发布:域名授权 编辑:程序博客网 时间:2024/06/05 07:19
 1<script type="text/javascript"> 2function check(){ 3//验证用户名 4var oUser=document.getElementById('username'); 5if(!/^[A-Z]{2}\d{3}$/.test(oUser.value)){ 6alert('用户名不正确'); 7oUser.select(); 8return false; 9} 10//验证密码 11var oPwd=document.getElementById('pwd'); 12if(!/^\d{6}$/.test(oPwd.value)){ 13alert('密码必须是6位的数字'); 14oPwd.select(); 15return false; 16} 17//性别必须是男或女 18var oSex=document.getElementById('sex'); 19if(!/^[男|女]$/.test(oSex.value)){ 20alert('性别必须是男或女'); 21oSex.select(); 22return false;n 23} 24//年龄 25/* 2610-120 2710-99100-120 28[1-9][0-9]  1[0-1][0-9]  120 29*/ 30var oAge=document.getElementById('age'); 31if(!/^[1-9][0-9]$|^1[0-1][0-9]$|^120$/.test(oAge.value)){ 32alert('年龄必须在10-120之间'); 33oAge.select(); 34return false; 35} 36//电话 37var oPhone=document.getElementById('phone'); 38if(!/^0[1-9]\d{1,2}-\d{7,8}$/.test(oPhone.value)){ 39alert('电话格式不正确'); 40oPhone.select(); 41return false; 42} 43//手机 44var oMobile=document.getElementById('mobile'); 45if(!/^1[3,4,5,7,8]\d{9}$/.test(oMobile.value)){ 46alert('手机格式不正确'); 47oMobile.select(); 48return false; 49} 50//邮箱 51var oEmail=document.getElementById('email'); 52if(!/^\w+@\w+\.\w+$|^\w+@\w+\.\w+\.\w+$/.test(oEmail.value)){ 53alert('邮箱格式不正确'); 54oEmail.select(); 55return false; 56} 57return true; 58} 59</script> 60<form action="" onsubmit='return check()'> 61<table align='center' width='500' > 62<tr> 63<td>用户名:</td> 64<td><input type="text" id="username"></td> 65</tr> 66<tr> 67<td>密码:</td> 68<td><input type="text" id="pwd"></td> 69</tr> 70<tr> 71<td>性别:</td> 72<td><input type="text" name="" id="sex"></td> 73</tr> 74<tr> 75<td>年龄:</td> 76<td><input type="text" name="" id="age"></td> 77</tr> 78<tr> 79<td>电话:</td> 80<td><input type="text" name="" id="phone"></td> 81</tr> 82<tr> 83<td>手机:</td> 84<td><input type="text" name="" id="mobile"></td> 85</tr> 86<tr> 87<td>邮箱:</td> 88<td><input type="text" name="" id="email"></td> 89</tr> 90<tr> 91<td colspan='2' align='center'> 92<input type="submit" value="提交"> 93</td> 94</tr> 95</table> 96</form>

1 0
原创粉丝点击