1、首先写一个form表单,里面的信息如下:<form method="post" id="form&鳎溻趄酃quot; action="xxx" onsubmi="return check_form()">用户名字:<input type="text" name="username" value=""><br>用户密码:<input type="password" name="password1" value=""><br>再次确认密码:<input type="text" name="password2" value=""><br>用户手机号:<input type="text" name="tel" value=""><br>用户身份证:<input type="text" name="idno" value=""><br>用户邮箱:<input type="text" name="mail" value=""><br><input type="submit" value="提交"><input type="reset" value="重置"></form>
2、首先是不为空的撰颧幌汪验证((用到了js技术):<script>functioncheck_form(){//获得表单的id var f=document.getElementById("form");//判断所有表单都不为空if(f.username.value==null||f.password1.value||f.password2.value||f.tel.value==null||f.idno.value==null||f.mail.value==null){alert("不可以有空值!");return false;}}</script>为方便大家看 我把这些验证 从一个方法里面分出来了!
3、判断身份证格式是否正确(用到了js里面的正则表达式)<script>functioncheck_form(){//获得表单的id var f=document.getElementById("form");//判断身份证的格式if(!/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(f.idno.value)){ alert("身份证号格式错误"); return false; }}</script>
4、判断用户手机号的格式是否正确:<script>functioncheck_form(){//获得表单的id var f=document.getElementById("form");//判断手机号码的格式if(!/^1[3458]\d{9}$/.test(f.tel.value)){ alert('手机号码格式不正确'); return false; }}</script>
5、//判断用户两次输入的密码是否一致<script>functioncheck_form(){//获得表单的id var f=document.getElementById("form");//判断两次输入的密码是否一致if(f.password1.value!=f.password2.value){ alert("两次输入的密码不一样,请重新输入!"); return false; }}</script>
6、//判断邮箱输入格式是否正确<script>functioncheck_form(){//获得表单的id var f=document.getElementById("form");var email=f.mail.value;if(email.search("^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))//邮箱验证正则表达式{return true;}else{return false}}</script>