graceChecker.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. 数据验证(表单验证)
  3. 来自 grace.hcoder.net
  4. 作者 hcoder 深海
  5. 版权声明 :
  6. GraceUI 的版权约束是不能转售或者将 GraceUI 直接发布到公开渠道!
  7. 侵权必究,请遵守版权约定!
  8. */
  9. module.exports = {
  10. error:'',
  11. check : function (dataBeCheck, rule){
  12. dataBeCheck = JSON.stringify(dataBeCheck);
  13. var data = JSON.parse(dataBeCheck);
  14. for(var i = 0; i < rule.length; i++){
  15. if (!rule[i].checkType){return true;}
  16. if (!rule[i].name) {return true;}
  17. if (!rule[i].errorMsg) {return true;}
  18. if (!data[rule[i].name] || data[rule[i].name] == '') {this.error = rule[i].errorMsg; return false;}
  19. // 检查前去除内容的空格及换行
  20. if(typeof(data[rule[i].name]) == 'string'){data[rule[i].name] = data[rule[i].name].replace(/\s/g,"");}
  21. switch (rule[i].checkType){
  22. case 'string':
  23. var reg = new RegExp('^.{' + rule[i].checkRule + '}$');
  24. if(!reg.test(data[rule[i].name])) {this.error = rule[i].errorMsg; return false;}
  25. break;
  26. case 'int':
  27. var ruleArr = rule[i].checkRule.split(',');
  28. if(rule.length < 2){
  29. ruleArr[0] = Number(ruleArr[0]) - 1;
  30. ruleArr[1] = '';
  31. }else{
  32. ruleArr[0] = Number(ruleArr[0]) - 1;
  33. ruleArr[1] = Number(ruleArr[1]) - 1;
  34. }
  35. var reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + ruleArr[0] + ',' + ruleArr[1] + '}$');
  36. if(!reg.test(data[rule[i].name])) {this.error = rule[i].errorMsg; return false;}
  37. break;
  38. break;
  39. case 'between':
  40. if (!this.isNumber(data[rule[i].name])){
  41. this.error = rule[i].errorMsg;
  42. return false;
  43. }
  44. var minMax = rule[i].checkRule.split(',');
  45. minMax[0] = Number(minMax[0]);
  46. minMax[1] = Number(minMax[1]);
  47. if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
  48. this.error = rule[i].errorMsg;
  49. return false;
  50. }
  51. break;
  52. case 'betweenD':
  53. var reg = /^-?\d+$/;
  54. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  55. var minMax = rule[i].checkRule.split(',');
  56. minMax[0] = Number(minMax[0]);
  57. minMax[1] = Number(minMax[1]);
  58. if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
  59. this.error = rule[i].errorMsg;
  60. return false;
  61. }
  62. break;
  63. case 'betweenF':
  64. var reg = /^-?[0-9][0-9]?.+[0-9]+$/;
  65. if (!reg.test(data[rule[i].name])){this.error = rule[i].errorMsg; return false;}
  66. var minMax = rule[i].checkRule.split(',');
  67. minMax[0] = Number(minMax[0]);
  68. minMax[1] = Number(minMax[1]);
  69. if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
  70. this.error = rule[i].errorMsg;
  71. return false;
  72. }
  73. break;
  74. case 'same':
  75. if (data[rule[i].name] != rule[i].checkRule) { this.error = rule[i].errorMsg; return false;}
  76. break;
  77. case 'notsame':
  78. if (data[rule[i].name] == rule[i].checkRule) { this.error = rule[i].errorMsg; return false; }
  79. break;
  80. case 'email':
  81. var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  82. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  83. break;
  84. case 'phoneno':
  85. var reg = /^1[0-9]{10,10}$/;
  86. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  87. break;
  88. case 'zipcode':
  89. var reg = /^[0-9]{6}$/;
  90. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  91. break;
  92. case 'reg':
  93. var reg = new RegExp(rule[i].checkRule);
  94. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  95. break;
  96. case 'in':
  97. if(rule[i].checkRule.indexOf(data[rule[i].name]) == -1){
  98. this.error = rule[i].errorMsg; return false;
  99. }
  100. break;
  101. case 'notnull':
  102. if(data[rule[i].name] == null || data[rule[i].name].length < 1){this.error = rule[i].errorMsg; return false;}
  103. break;
  104. case 'samewith':
  105. if(data[rule[i].name] != data[rule[i].checkRule]){this.error = rule[i].errorMsg; return false;}
  106. break;
  107. case 'numbers':
  108. var reg = new RegExp('^[0-9]{' + rule[i].checkRule + '}$');
  109. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  110. break;
  111. }
  112. }
  113. return true;
  114. },
  115. isNumber : function (checkVal){
  116. checkVal = Number(checkVal);
  117. if(checkVal == NaN){return false;}
  118. return true;
  119. }
  120. }