reg-verify.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. (function(global,factory){
  2. global['$reg'] = factory();
  3. typeof define == 'function' && define('reg-verify',[],factory);
  4. })(this,function(module,exports){
  5. return {
  6. isHref:function(test){
  7. return /^http(s)?:\/\/([\w-]+\.)+[\w\-]+(\/[\w\- .\/?%&=]*)?$/.exec(test) !== null;
  8. },
  9. isEmail:function(test){
  10. var reg = /\w+[@]{1}\w+[.]\w+/;
  11. return reg.test(test);
  12. },
  13. isPhone:function(test){
  14. var reg = /^1[3456789]\d{9}$/;
  15. return reg.test(test);
  16. },
  17. isPostCode:function(test){
  18. var reg = /^[0-9]\d{5}$/;
  19. return reg.test(test);
  20. },
  21. isEmpty:function(test){
  22. test = this.trim(test);
  23. return test == null || test == '' || test == undefined
  24. },
  25. isArray:function(test){
  26. return Object.prototype.toString.call(test) == '[object Array]';
  27. },
  28. isObject:function(test){
  29. return Object.prototype.toString.call(test) == '[object Object]';
  30. },
  31. isUndefined:function(test){
  32. return Object.prototype.toString.call(test) == '[object Undefined]';
  33. },
  34. isNull:function(test){
  35. return Object.prototype.toString.call(test) == '[object Null]';
  36. },
  37. trim:function(test){
  38. return test.toString().replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  39. },
  40. isCard:function (code) {
  41. var city={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:"国外 "};
  42. var tip = "";
  43. var pass= true;
  44. if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
  45. tip = "身份证号格式错误";
  46. pass = false;
  47. }
  48. else if(!city[code.substr(0,2)]){
  49. tip = "地址编码错误";
  50. pass = false;
  51. }
  52. else{
  53. //18位身份证需要验证最后一位校验位
  54. if(code.length == 18){
  55. code = code.split('');
  56. //∑(ai×Wi)(mod 11)
  57. //加权因子
  58. var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
  59. //校验位
  60. var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
  61. var sum = 0;
  62. var ai = 0;
  63. var wi = 0;
  64. for (var i = 0; i < 17; i++)
  65. {
  66. ai = code[i];
  67. wi = factor[i];
  68. sum += ai * wi;
  69. }
  70. var last = parity[sum % 11];
  71. if(parity[sum % 11] != code[17]){
  72. tip = "校验位错误";
  73. pass =false;
  74. }
  75. }else{
  76. pass =false;
  77. }
  78. }
  79. if(!pass) return false;/* 身份证格式错误*/
  80. return true;/* 身份证格式正确*/
  81. }
  82. }
  83. });