validate-methods.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*****************************************************************
  2. * jQuery Validate扩展验证方法 (linjq)
  3. * Modified by guojunhui
  4. * Date modified:01/01/2017
  5. *****************************************************************/
  6. $(function(){
  7. // 判断整数value是否等于0
  8. jQuery.validator.addMethod("isIntEqZero", function(value, element) {
  9. value=parseInt(value);
  10. return this.optional(element) || value==0;
  11. }, "整数必须为0");
  12. // 判断整数value是否大于0
  13. jQuery.validator.addMethod("isIntGtZero", function(value, element) {
  14. value=parseInt(value);
  15. return this.optional(element) || value>0;
  16. }, "整数必须大于0");
  17. // 判断整数value是否大于或等于0
  18. jQuery.validator.addMethod("isIntGteZero", function(value, element) {
  19. value=parseInt(value);
  20. return this.optional(element) || value>=0;
  21. }, "整数必须大于或等于0");
  22. // 判断整数value是否不等于0
  23. jQuery.validator.addMethod("isIntNEqZero", function(value, element) {
  24. value=parseInt(value);
  25. return this.optional(element) || value!=0;
  26. }, "整数必须不等于0");
  27. // 判断整数value是否小于0
  28. jQuery.validator.addMethod("isIntLtZero", function(value, element) {
  29. value=parseInt(value);
  30. return this.optional(element) || value<0;
  31. }, "整数必须小于0");
  32. // 判断整数value是否小于或等于0
  33. jQuery.validator.addMethod("isIntLteZero", function(value, element) {
  34. value=parseInt(value);
  35. return this.optional(element) || value<=0;
  36. }, "整数必须小于或等于0");
  37. // 判断浮点数value是否等于0
  38. jQuery.validator.addMethod("isFloatEqZero", function(value, element) {
  39. value=parseFloat(value);
  40. return this.optional(element) || value==0;
  41. }, "浮点数必须为0");
  42. // 判断浮点数value是否大于0
  43. jQuery.validator.addMethod("isFloatGtZero", function(value, element) {
  44. value=parseFloat(value);
  45. return this.optional(element) || value>0;
  46. }, "浮点数必须大于0");
  47. // 判断浮点数value是否大于或等于0
  48. jQuery.validator.addMethod("isFloatGteZero", function(value, element) {
  49. value=parseFloat(value);
  50. return this.optional(element) || value>=0;
  51. }, "浮点数必须大于或等于0");
  52. // 判断浮点数value是否不等于0
  53. jQuery.validator.addMethod("isFloatNEqZero", function(value, element) {
  54. value=parseFloat(value);
  55. return this.optional(element) || value!=0;
  56. }, "浮点数必须不等于0");
  57. // 判断浮点数value是否小于0
  58. jQuery.validator.addMethod("isFloatLtZero", function(value, element) {
  59. value=parseFloat(value);
  60. return this.optional(element) || value<0;
  61. }, "浮点数必须小于0");
  62. // 判断浮点数value是否小于或等于0
  63. jQuery.validator.addMethod("isFloatLteZero", function(value, element) {
  64. value=parseFloat(value);
  65. return this.optional(element) || value<=0;
  66. }, "浮点数必须小于或等于0");
  67. // 判断浮点型
  68. jQuery.validator.addMethod("isFloat", function(value, element) {
  69. return this.optional(element) || /^[-\+]?\d+(\.\d+)?$/.test(value);
  70. }, "只能包含数字、小数点等字符");
  71. // 匹配integer
  72. jQuery.validator.addMethod("isInteger", function(value, element) {
  73. return this.optional(element) || (/^[-\+]?\d+$/.test(value) && parseInt(value)>=0);
  74. }, "匹配integer");
  75. // 判断数值类型,包括整数和浮点数
  76. jQuery.validator.addMethod("isNumber", function(value, element) {
  77. return this.optional(element) || /^[-\+]?\d+$/.test(value) || /^[-\+]?\d+(\.\d+)?$/.test(value);
  78. }, "匹配数值类型,包括整数和浮点数");
  79. // 只能输入[0-9]数字
  80. jQuery.validator.addMethod("isDigits", function(value, element) {
  81. return this.optional(element) || /^\d+$/.test(value);
  82. }, "只能输入0-9数字");
  83. // 判断中文字符
  84. jQuery.validator.addMethod("isChinese", function(value, element) {
  85. return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);
  86. }, "只能包含中文字符。");
  87. // 判断英文字符
  88. jQuery.validator.addMethod("isEnglish", function(value, element) {
  89. return this.optional(element) || /^[A-Za-z]+$/.test(value);
  90. }, "只能包含英文字符。");
  91. // 手机号码验证
  92. jQuery.validator.addMethod("isMobile", function(value, element) {
  93. var length = value.length;
  94. return this.optional(element) || (length == 11 && /^(((13[0-9]{1})|(15[0-35-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(value));
  95. }, "手机号码格式不正确。");
  96. // 电话号码验证
  97. jQuery.validator.addMethod("isPhone", function(value, element) {
  98. var tel = /^(\d{3,4}-?)?\d{7,9}$/g;
  99. return this.optional(element) || (tel.test(value));
  100. }, "电话号码格式不正确");
  101. // 联系电话(手机/电话皆可)验证
  102. jQuery.validator.addMethod("isTel", function(value,element) {
  103. var length = value.length;
  104. var mobile = /^(((13[0-9]{1})|(15[0-35-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
  105. var tel = /^(\d{3,4}-?)?\d{7,9}$/g;
  106. return this.optional(element) || tel.test(value) || (length==11 && mobile.test(value));
  107. }, "请输入正确手机号码或电话号码");
  108. // 匹配qq
  109. jQuery.validator.addMethod("isQq", function(value, element) {
  110. return this.optional(element) || /^[1-9]\d{4,12}$/;
  111. }, "QQ号码不合法");
  112. // 邮政编码验证
  113. jQuery.validator.addMethod("isZipCode", function(value, element) {
  114. var zip = /^[0-9]{6}$/;
  115. return this.optional(element) || (zip.test(value));
  116. }, "邮政编码不正确");
  117. // 匹配密码,以字母开头,长度在6-16之间,只能包含字符、数字和下划线。
  118. jQuery.validator.addMethod("isPwd", function(value, element) {
  119. return this.optional(element) || /^[a-zA-Z]\\w{6,16}$/.test(value);
  120. }, "以字母开头,长度在6-12之间,只能包含字符、数字和下划线。");
  121. // 身份证号码验证
  122. jQuery.validator.addMethod("isIdCardNo", function(value, element) {
  123. //var idCard = /^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/;
  124. return this.optional(element) || isIdCardNo(value);
  125. }, "身份证号码不正确");
  126. // IP地址验证
  127. jQuery.validator.addMethod("ip", function(value, element) {
  128. return this.optional(element) || /^(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.)(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.){2}([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))$/.test(value);
  129. }, "请填写正确的IP地址");
  130. // 字符验证,只能包含中文、英文、数字、下划线等字符。
  131. jQuery.validator.addMethod("stringCheck", function(value, element) {
  132. return this.optional(element) || /^[a-zA-Z0-9\u4e00-\u9fa5-_]+$/.test(value);
  133. }, "只能包含中文、英文、数字、下划线等字符");
  134. // 匹配english
  135. jQuery.validator.addMethod("isEnglish", function(value, element) {
  136. return this.optional(element) || /^[A-Za-z]+$/.test(value);
  137. }, "必须输入英文");
  138. // 匹配汉字
  139. jQuery.validator.addMethod("isChinese", function(value, element) {
  140. return this.optional(element) || /^[\u4e00-\u9fa5]+$/.test(value);
  141. }, "只能输入汉字");
  142. // 匹配中文(包括汉字和字符)
  143. jQuery.validator.addMethod("isChineseChar", function(value, element) {
  144. return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);
  145. }, "匹配中文(包括汉字和字符) ");
  146. // 判断是否为合法字符(a-zA-Z0-9-_)
  147. jQuery.validator.addMethod("isRightfulString", function(value, element) {
  148. return this.optional(element) || /^[A-Za-z0-9_-]+$/.test(value);
  149. }, "判断是否为合法字符(a-zA-Z0-9-_)");
  150. // 判断是否包含中英文特殊字符,除英文"-_"字符外
  151. jQuery.validator.addMethod("isContainsSpecialChar", function(value, element) {
  152. var reg = RegExp(/[(\ )(\`)(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\+)(\=)(\|)(\{)(\})(\')(\:)(\;)(\')(',)(\[)(\])(\.)(\<)(\>)(\/)(\?)(\~)(\!)(\@)(\#)(\¥)(\%)(\…)(\&)(\*)(\()(\))(\—)(\+)(\|)(\{)(\})(\【)(\】)(\‘)(\;)(\:)(\”)(\“)(\’)(\。)(\,)(\、)(\?)]+/);
  153. return this.optional(element) || !reg.test(value);
  154. }, "含有中英文特殊字符");
  155. //车牌号校验
  156. jQuery.validator.addMethod("isPlateNo", function(value, element) {
  157. var reg = /^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$/;
  158. return this.optional(element) || (tel.test(value));
  159. },"请输入正确车牌号");
  160. });
  161. //身份证号码的验证规则
  162. function isIdCardNo(num){
  163.   //if (isNaN(num)) {alert("输入的不是数字!"); return false;}
  164.    var len = num.length, re;
  165.    if (len == 15)
  166.    re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{2})(\w)$/);
  167.    else if (len == 18)
  168.    re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/);
  169.    else {
  170. //alert("输入的数字位数不对。");
  171. return false;
  172. }
  173.    var a = num.match(re);
  174.    if (a != null)
  175.    {
  176.    if (len==15)
  177.    {
  178.    var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);
  179.    var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
  180.    }
  181.    else
  182.    {
  183.    var D = new Date(a[3]+"/"+a[4]+"/"+a[5]);
  184.    var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
  185.    }
  186.    if (!B) {
  187. //alert("输入的身份证号 "+ a[0] +" 里出生日期不对。");
  188. return false;
  189. }
  190.    }
  191.    if(!re.test(num)){
  192. //alert("身份证最后一位只能是数字和字母。");
  193. return false;
  194. }
  195.    return true;
  196. }