formChecker.js 3.2 KB

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