graceChecker.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* eslint-disable */
  2. /**
  3. *@des 表单验证
  4. *@author hjp1011 21931118@qq.com
  5. *@blog http://www.wephp.com
  6. *@date 2019/11/15 10:55:56
  7. */
  8. module.exports = {
  9. error: '',
  10. check: function(data, rule) {
  11. for (let i = 0; i < rule.length; i++) {
  12. if (!rule[i].checkType) {
  13. return true;
  14. }
  15. if (!rule[i].name) {
  16. return true;
  17. }
  18. if (!rule[i].errorMsg) {
  19. return true;
  20. }
  21. if (!data[rule[i].name]) {
  22. this.error = rule[i].errorMsg;
  23. return false;
  24. }
  25. switch (rule[i].checkType) {
  26. case 'string':
  27. var reg = new RegExp('^.{' + rule[i].checkRule + '}$');
  28. if (!reg.test(data[rule[i].name])) {
  29. this.error = rule[i].errorMsg;
  30. return false;
  31. }
  32. break;
  33. case 'int':
  34. var reg = new RegExp(
  35. '^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$'
  36. );
  37. if (!reg.test(data[rule[i].name])) {
  38. this.error = rule[i].errorMsg;
  39. return false;
  40. }
  41. break;
  42. break;
  43. case 'between':
  44. if (!this.isNumber(data[rule[i].name])) {
  45. this.error = rule[i].errorMsg;
  46. return false;
  47. }
  48. var minMax = rule[i].checkRule.split(',');
  49. minMax[0] = Number(minMax[0]);
  50. minMax[1] = Number(minMax[1]);
  51. if (
  52. data[rule[i].name] > minMax[1] ||
  53. data[rule[i].name] < minMax[0]
  54. ) {
  55. this.error = rule[i].errorMsg;
  56. return false;
  57. }
  58. break;
  59. case 'betweenD':
  60. var reg = /^-?[1-9][0-9]?$/;
  61. if (!reg.test(data[rule[i].name])) {
  62. this.error = rule[i].errorMsg;
  63. return false;
  64. }
  65. var minMax = rule[i].checkRule.split(',');
  66. minMax[0] = Number(minMax[0]);
  67. minMax[1] = Number(minMax[1]);
  68. if (
  69. data[rule[i].name] > minMax[1] ||
  70. data[rule[i].name] < minMax[0]
  71. ) {
  72. this.error = rule[i].errorMsg;
  73. return false;
  74. }
  75. break;
  76. case 'betweenF':
  77. var reg = /^-?[0-9][0-9]?.+[0-9]+$/;
  78. if (!reg.test(data[rule[i].name])) {
  79. this.error = rule[i].errorMsg;
  80. return false;
  81. }
  82. var minMax = rule[i].checkRule.split(',');
  83. minMax[0] = Number(minMax[0]);
  84. minMax[1] = Number(minMax[1]);
  85. if (
  86. data[rule[i].name] > minMax[1] ||
  87. data[rule[i].name] < minMax[0]
  88. ) {
  89. this.error = rule[i].errorMsg;
  90. return false;
  91. }
  92. break;
  93. case 'same':
  94. if (data[rule[i].name] != rule[i].checkRule) {
  95. this.error = rule[i].errorMsg;
  96. return false;
  97. }
  98. break;
  99. case 'notsame':
  100. if (data[rule[i].name] == rule[i].checkRule) {
  101. this.error = rule[i].errorMsg;
  102. return false;
  103. }
  104. break;
  105. case 'email':
  106. var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  107. if (!reg.test(data[rule[i].name])) {
  108. this.error = rule[i].errorMsg;
  109. return false;
  110. }
  111. break;
  112. case 'phoneno':
  113. var reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
  114. if (!reg.test(data[rule[i].name])) {
  115. this.error = rule[i].errorMsg;
  116. return false;
  117. }
  118. break;
  119. case 'zipcode':
  120. var reg = /^[0-9]{6}$/;
  121. if (!reg.test(data[rule[i].name])) {
  122. this.error = rule[i].errorMsg;
  123. return false;
  124. }
  125. break;
  126. case 'reg':
  127. var reg = new RegExp(rule[i].checkRule);
  128. if (!reg.test(data[rule[i].name])) {
  129. this.error = rule[i].errorMsg;
  130. return false;
  131. }
  132. break;
  133. case 'in':
  134. if (rule[i].checkRule.indexOf(data[rule[i].name]) == -1) {
  135. this.error = rule[i].errorMsg;
  136. return false;
  137. }
  138. break;
  139. case 'notnull':
  140. if (data[rule[i].name] == null || data[rule[i].name].length < 1) {
  141. this.error = rule[i].errorMsg;
  142. return false;
  143. }
  144. break;
  145. }
  146. }
  147. return true;
  148. },
  149. isNumber: function(checkVal) {
  150. let reg = /^-?[1-9][0-9]?.?[0-9]*$/;
  151. return reg.test(checkVal);
  152. }
  153. };