jquery.validate 自定义验证方法

引用


jquery.validate 自定义验证方法 
Java代码   收藏代码
  1. $(document).ready( function() {  
  2.   
  3. /** 
  4.  * 身份证号码验证 
  5.  * 
  6.  */  
  7. function isIdCardNo(num) {  
  8.   
  9.  var factorArr = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);  
  10.  var parityBit=new Array("1","0","X","9","8","7","6","5","4","3","2");  
  11.  var varArray = new Array();  
  12.  var intValue;  
  13.  var lngProduct = 0;  
  14.  var intCheckDigit;  
  15.  var intStrLen = num.length;  
  16.  var idNumber = num;  
  17.    // initialize  
  18.      if ((intStrLen != 15) && (intStrLen != 18)) {  
  19.          return false;  
  20.      }  
  21.      // check and set value  
  22.      for(i=0;i<intStrLen;i++) {  
  23.          varArray[i] = idNumber.charAt(i);  
  24.          if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {  
  25.              return false;  
  26.          } else if (i < 17) {  
  27.              varArray[i] = varArray[i] * factorArr[i];  
  28.          }  
  29.      }  
  30.        
  31.      if (intStrLen == 18) {  
  32.          //check date  
  33.          var date8 = idNumber.substring(6,14);  
  34.          if (isDate8(date8) == false) {  
  35.             return false;  
  36.          }  
  37.          // calculate the sum of the products  
  38.          for(i=0;i<17;i++) {  
  39.              lngProduct = lngProduct + varArray[i];  
  40.          }  
  41.          // calculate the check digit  
  42.          intCheckDigit = parityBit[lngProduct % 11];  
  43.          // check last digit  
  44.          if (varArray[17] != intCheckDigit) {  
  45.              return false;  
  46.          }  
  47.      }  
  48.      else{        //length is 15  
  49.          //check date  
  50.          var date6 = idNumber.substring(6,12);  
  51.          if (isDate6(date6) == false) {  
  52.   
  53.              return false;  
  54.          }  
  55.      }  
  56.      return true;  
  57.        
  58.  }  
  59. /** 
  60.  * 判断是否为“YYYYMM”式的时期 
  61.  * 
  62.  */  
  63. function isDate6(sDate) {  
  64.    if(!/^[0-9]{6}$/.test(sDate)) {  
  65.       return false;  
  66.    }  
  67.    var year, month, day;  
  68.    year = sDate.substring(04);  
  69.    month = sDate.substring(46);  
  70.    if (year < 1700 || year > 2500return false  
  71.    if (month < 1 || month > 12return false  
  72.    return true  
  73. }  
  74. /** 
  75.  * 判断是否为“YYYYMMDD”式的时期 
  76.  * 
  77.  */  
  78. function isDate8(sDate) {  
  79.    if(!/^[0-9]{8}$/.test(sDate)) {  
  80.       return false;  
  81.    }  
  82.    var year, month, day;  
  83.    year = sDate.substring(04);  
  84.    month = sDate.substring(46);  
  85.    day = sDate.substring(68);  
  86.    var iaMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31]  
  87.    if (year < 1700 || year > 2500return false  
  88.    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1]=29;  
  89.    if (month < 1 || month > 12return false  
  90.    if (day < 1 || day > iaMonthDays[month - 1]) return false  
  91.    return true  
  92. }  
  93.  // 身份证号码验证     
  94.  jQuery.validator.addMethod("idcardno", function(value, element) {  
  95.    return this.optional(element) || isIdCardNo(value);     
  96.  }, "请正确输入身份证号码");  
  97.    
  98.   //字母数字  
  99.  jQuery.validator.addMethod("alnum", function(value, element) {  
  100.    return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);  
  101.  }, "只能包括英文字母和数字");  
  102.    
  103.  // 邮政编码验证  
  104.  jQuery.validator.addMethod("zipcode", function(value, element) {  
  105.    var tel = /^[0-9]{6}$/;  
  106.    return this.optional(element) || (tel.test(value));  
  107.  }, "请正确填写邮政编码");  
  108.    
  109.   // 汉字  
  110.  jQuery.validator.addMethod("chcharacter", function(value, element) {  
  111.    var tel = /^[\u4e00-\u9fa5]+$/;  
  112.    return this.optional(element) || (tel.test(value));  
  113.  }, "请输入汉字");  
  114.   
  115. // 字符最小长度验证(一个中文字符长度为2)  
  116. jQuery.validator.addMethod("stringMinLength", function(value, element, param) {  
  117. var length = value.length;  
  118. for ( var i = 0; i < value.length; i++) {  
  119. if (value.charCodeAt(i) > 
分享到:
评论

你可能感兴趣的:(jquery)