验证表单的js代码段

Posted 浪迹天涯芳草

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证表单的js代码段相关的知识,希望对你有一定的参考价值。

 
转载自:http://blog.csdn.net/kalision/article/details/12516103
引用js文件:

<script src="js/demo.js"></script>

 

按钮

  <button type="button" class="button medium button-style1 align-btn-right" value="send" onclick="checkSubmit();">提交</button>

 

js文件内容:

 

[html] view plain copy
 
 
  1.     var b = new Boolean();    
  2.     b = false;    
  3.     var b2 = new Boolean();    
  4.     b2 = false;    
  5.     function checkSubmit(){  
  6.         var bbxm = document.getElementById("bbxm").value;  
  7.         var bbxb = document.getElementById("bbxb").value;  
  8.         var csrq = document.getElementById("csrq").value;  
  9.           
  10.         if(bbxm != ‘‘){  
  11.             if(bbxb != ‘‘){  
  12.                 if(csrq != ‘‘){  
  13.                     if(isIdCardNo()){  
  14.                         document.form1.submit();    
  15.                     }else{  
  16.                         document.getElementById("identityid").focus();  
  17.                     }  
  18.                 }else{  
  19.                     alert("请选择出生日期!");  
  20.                     document.getElementById("csrq").focus();  
  21.                 }  
  22.             }else{  
  23.                 alert("请选择性别!");  
  24.                 document.getElementById("bbxb").focus();  
  25.             }  
  26.         }else{  
  27.             alert("姓名不能为空!");    
  28.             document.getElementById("bbxm").focus();  
  29.         }  
  30.     }    
  31.         
  32.     function isPassWord(passWord,passWordDemo){    
  33.         
  34.         var passWord = document.getElementById(passWord).value;    
  35.         var passWordDemo = document.getElementById(passWordDemo).value;    
  36.     
  37.             
  38.         if(passWordDemo != passWord){    
  39.             document.getElementById(‘spantest2‘).innerText = "两次输入的密码不一致! ";    
  40.             b2 = false;    
  41.             return false;    
  42.         }else{    
  43.             document.getElementById(‘spantest2‘).innerText = "正确";    
  44.             b2 = true;    
  45.             return true;    
  46.         }    
  47.     }    
  48.         
  49.     function isEmail(email) {     
  50.     var strEmail=document.getElementById(email).value;     
  51.     if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)     
  52.     {     
  53.             document.getElementById(‘spantest‘).innerText = "邮箱格式正确";    
  54.             b = true;    
  55.             return true;     
  56.     }else{     
  57.             document.getElementById(‘spantest‘).innerText = "邮箱格式错误!";    
  58.             document.getElementById(email).focus();     
  59.             b = false;    
  60.             return false;     
  61.     }     
  62.         
  63.     
  64. }   
  65. function validatek(){  
  66.       
  67. }  
  68. function isIdCardNo() {   
  69.             var tex = document.getElementById(‘identityid‘);    
  70.             var num = document.getElementById(‘identityid‘).value;    
  71.             if(num != ‘‘){  
  72.                 //alert("您输入的身份证号码是:\n" + num);    
  73.                 num = num.toUpperCase();      
  74.                 //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X。       
  75.                 if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num)))       
  76.                 {     
  77.                     alert(‘输入的身份证号长度不对,或者号码不符合规定!\n身份证号码为15位时,应全为数字,\n身份证号码为18位时,末位可以为数字或X。‘);      
  78.                     tex.focus();    
  79.                     return false;     
  80.                 }     
  81.                 //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。     
  82.                 //下面分别分析出生日期和校验位     
  83.                 var len, re;     
  84.                 len = num.length;    
  85.                     
  86.                 //当身份证为15位时的验证出生日期。    
  87.                 if (len == 15)     
  88.                 {     
  89.                     re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);     
  90.                     var arrSplit = num.match(re);     
  91.         
  92.                     //检查生日日期是否正确     
  93.                     var dtmBirth = new Date(‘19‘ + arrSplit[2] + ‘/‘ + arrSplit[3] + ‘/‘ + arrSplit[4]);     
  94.                     var bGoodDay;     
  95.                     bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));     
  96.                     if (!bGoodDay){     
  97.                         alert(‘输入的15位身份证号里出生日期不对!‘);       
  98.                         tex.focus();    
  99.                         return false;     
  100.                     }     
  101.                       
  102.                 }     
  103.                     
  104.                 //当身份证号为18位时,校验出生日期和校验位。    
  105.                 if (len == 18)     
  106.                 {     
  107.                     var year = num.substr(6,4);    
  108.                     //alert("year是:" + year);    
  109.                     var nowDate = new Date();    
  110.                     var nowYear = nowDate.getYear();    
  111.                     if((nowYear - year) > 112){    
  112.                         alert("依照输入的身份证出生日期截止到当前,不属于范围!");      
  113.                         tex.focus();    
  114.                         return false;    
  115.                     }    
  116.         
  117.                     re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);     
  118.                     var arrSplit = num.match(re);     
  119.         
  120.                     //检查生日日期是否正确     
  121.                     var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);     
  122.                     var bGoodDay;     
  123.                     bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));     
  124.                     if (!bGoodDay){     
  125.                         //alert(dtmBirth.getYear());     
  126.                         //alert(arrSplit[2]);     
  127.                         alert(‘输入的18位身份证号里出生日期不对!‘);      
  128.                         tex.focus();    
  129.                         return false;     
  130.                     }     
  131.                     else{     
  132.                         //检验18位身份证的校验码是否正确。     
  133.                         //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。     
  134.                         var valnum;     
  135.                         var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);     
  136.                         var arrCh = new Array(‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘);     
  137.                         var nTemp = 0, i;     
  138.                         for(i = 0; i 17; i ++){     
  139.                             nTemp += num.substr(i, 1) * arrInt[i];     
  140.                         }     
  141.                         valnum = arrCh[nTemp % 11];     
  142.                         if (valnum != num.substr(17, 1)){     
  143.                             alert(‘18位身份证的最后一位校验码不正确!‘); //应该为:‘ + valnum    
  144.                             tex.value=‘‘;    
  145.                             tex.focus();    
  146.                             return false;     
  147.                         }    
  148.                       
  149.                     }     
  150.                 }     
  151.                     
  152.                 //验证地区是否有效    
  153.                 var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "}    
  154.                 if(aCity[parseInt(num.substr(0,2))] == null){    
  155.                     alert("输入的身份证号前两位地区不对!");    
  156.                     tex.value=‘‘;    
  157.                     tex.focus();    
  158.                     return false;    
  159.                 }  
  160.                 return true;  
  161.             } else{  
  162.                 return true;  
  163.             }  
  164.                            

以上是关于验证表单的js代码段的主要内容,如果未能解决你的问题,请参考以下文章

使用 parsley.js 验证表单提交外部表单标记

js表单验证

我要一段验证身份证的js加上表单的写法

JS表单验证-12个常用的JS表单验证

JS表单验证

js表单验证