graceChecker.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. 数据验证(表单验证)
  3. 来自 grace.hcoder.net
  4. 作者 hcoder 深海
  5. */
  6. module.exports = {
  7. error:'',
  8. check : function (data, rule){
  9. for(let i = 0; i < rule.length; i++){
  10. if (!rule[i].checkType){return true;}
  11. if (!rule[i].name) {return true;}
  12. if (!rule[i].errorMsg) {return true;}
  13. if (!data[rule[i].name]) {this.error = rule[i].errorMsg; return false;}
  14. switch (rule[i].checkType){
  15. case 'string':
  16. var reg = new RegExp('^.{' + rule[i].checkRule + '}$');
  17. if(!reg.test(data[rule[i].name])) {this.error = rule[i].errorMsg; return false;}
  18. break;
  19. case 'int':
  20. var reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$');
  21. if(!reg.test(data[rule[i].name])) {this.error = rule[i].errorMsg; return false;}
  22. break;
  23. break;
  24. case 'between':
  25. if (!this.isNumber(data[rule[i].name])){
  26. this.error = rule[i].errorMsg;
  27. return false;
  28. }
  29. var minMax = rule[i].checkRule.split(',');
  30. minMax[0] = Number(minMax[0]);
  31. minMax[1] = Number(minMax[1]);
  32. if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
  33. this.error = rule[i].errorMsg;
  34. return false;
  35. }
  36. break;
  37. case 'betweenD':
  38. var reg = /^-?[1-9][0-9]?$/;
  39. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  40. var minMax = rule[i].checkRule.split(',');
  41. minMax[0] = Number(minMax[0]);
  42. minMax[1] = Number(minMax[1]);
  43. if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
  44. this.error = rule[i].errorMsg;
  45. return false;
  46. }
  47. break;
  48. case 'betweenF':
  49. var reg = /^-?[0-9][0-9]?.+[0-9]+$/;
  50. if (!reg.test(data[rule[i].name])){this.error = rule[i].errorMsg; return false;}
  51. var minMax = rule[i].checkRule.split(',');
  52. minMax[0] = Number(minMax[0]);
  53. minMax[1] = Number(minMax[1]);
  54. if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
  55. this.error = rule[i].errorMsg;
  56. return false;
  57. }
  58. break;
  59. case 'same':
  60. if (data[rule[i].name] != rule[i].checkRule) { this.error = rule[i].errorMsg; return false;}
  61. break;
  62. case 'notsame':
  63. if (data[rule[i].name] == rule[i].checkRule) { this.error = rule[i].errorMsg; return false; }
  64. break;
  65. case 'email':
  66. var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  67. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  68. break;
  69. case 'phoneno':
  70. var reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
  71. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  72. break;
  73. case 'zipcode':
  74. var reg = /^[0-9]{6}$/;
  75. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  76. break;
  77. case 'reg':
  78. var reg = new RegExp(rule[i].checkRule);
  79. if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false; }
  80. break;
  81. case 'in':
  82. if(rule[i].checkRule.indexOf(data[rule[i].name]) == -1){
  83. this.error = rule[i].errorMsg; return false;
  84. }
  85. break;
  86. case 'notnull':
  87. if(data[rule[i].name] == null || data[rule[i].name].length < 1){this.error = rule[i].errorMsg; return false;}
  88. break;
  89. }
  90. }
  91. return true;
  92. },
  93. isNumber : function (checkVal){
  94. let reg = /^-?[1-9][0-9]?.?[0-9]*$/;
  95. return reg.test(checkVal);
  96. }
  97. }