注册校验小例子

来源:互联网 发布:linux home代表什么 编辑:程序博客网 时间:2024/05/27 00:31
<script Language="JavaScript"> function formCheck() { if (document.forms[0].userName.value==""){alert("请填写您的用户名!");document.forms[0].userName.focus();return false;}var filter=/^s*[A-Za-z0-9]{6,20}s*$/;if (!filter.test(document.forms[0].userName.value)){ alert("用户名填写不正确,请重新填写!可使用的字符为(A-Z a-z 0-9)长度不小于6个字符,不超过20个字符,注意不要使用空格。"); document.forms[0].userName.focus();document.forms[0].userName.select();return false; } if (document.forms[0].password.value=="") {alert("请填写您的密码!");document.forms[0].password.focus();return false; }var filter=/^s*[A-Za-z0-9]{6,20}s*$/;if (!filter.test(document.forms[0].password.value)){ alert("密码填写不正确,请重新填写!可使用的字符为(A-Z a-z 0-9 )长度不小于6个字符,不超过20个字符,注意不要使用空格。"); document.forms[0].password.focus();document.forms[0].password.select();return false; } if (document.forms[0].pwd.value==""){alert("请输入您的确认密码!");document.forms[0].pwd.focus();return false;}if (document.forms[0].password.value!=document.forms[0].pwd.value){alert("两次填写的密码不一致,请重新填写!"); document.forms[0].password.focus();document.forms[0].password.select();return false; } if (document.forms[0].name.value==""){alert("请填写您的真实姓名!");document.forms[0].name.focus();return false;}var patrn=/^[\u0391-\uFFE5]{2,4}$/; //正规表达式的判断是否是中文if (!patrn.test(document.forms[0].name.value)){alert("请输入真实姓名,2-4个汉字!"); document.forms[0].name.focus();document.forms[0].name.select();return false; } if (document.forms[0].age.value==""){alert("请输入您的年龄!");document.forms[0].age.focus();return false;}var number=/^[0-9]{1,3}$/; if (!number.test(document.forms[0].age.value)){alert("请输入您的年龄(必须为数字)!");document.forms[0].age.focus();return false;}if (document.forms[0].stature.value==""){alert("请填写您的身高!");document.forms[0].stature.focus();return false;}var stature=/^[0-9]{1,3}$/; if (!stature.test(document.forms[0].stature.value)){alert("请输入您的身高(必须为数字)!");document.forms[0].stature.focus();return false;}if (document.forms[0].tel.value==""){alert("请填写您的联系电话!");document.forms[0].tel.focus();return false;}var tel =/(^([0][1-9]{2,3}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0][1-9]{2,3}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$)|(^[1][3][0-9]{9}$)|(^0[1][3][0-9]{9}$)/;  if (!tel.test(document.forms[0].tel.value)) { alert("请输入正确的电话号码(手机号码)!");return false; }if(document.forms[0].mail.value==""){alert("请输入您的E-Mail!");document.forms[0].mail.focus();return false;}var mail = /^([-_A-Za-z0-9\.]+)@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/; if(!mail.test(document.forms[0].mail.value)){ alert("E-mail格式不正确,请重新填写!"); document.forms[0].mail.focus();document.forms[0].mail.select();return false; } if(document.forms[0].school.value==""){alert("请输入您的学校!");document.forms[0].school.focus();return false;}var school=/^[\u0391-\uFFE5]{1,20}$/; if (!school.test(document.forms[0].school.value)){alert("请输入您的学校(必须为汉字)!"); document.forms[0].school.focus();document.forms[0].school.select();return false; } return true;}</script>




提交表单:
<form action="#" method="post" onsubmit="return formCheck()">

注意:
document.forms[0].userName.value中的forms[0]就是你此网页中的第一个form表单,依次类推;userName就是你输入文本框的名字(<input type="text" name="userName" value="">);
原创粉丝点击