graceChecker.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. 数据验证(表单验证)
  3. 来自 grace.hcoder.net
  4. 作者 hcoder 深海
  5. */
  6. module.exports = {
  7. error: '',
  8. check: function(data, rule) {
  9. for (var i = 0; i < rule.length; i++) {
  10. if (!rule[i].checkType) {
  11. return true;
  12. }
  13. if (!rule[i].name) {
  14. return true;
  15. }
  16. if (!rule[i].errorMsg) {
  17. return true;
  18. }
  19. switch (rule[i].checkType) {
  20. case 'string':
  21. var reg = new RegExp('^.{' + rule[i].checkRule + '}$');
  22. if (!reg.test(data[rule[i].name])) {
  23. this.error = rule[i].errorMsg;
  24. return false;
  25. }
  26. break;
  27. case 'int':
  28. var reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$');
  29. if (!reg.test(data[rule[i].name])) {
  30. this.error = rule[i].errorMsg;
  31. return false;
  32. }
  33. break;
  34. case 'between':
  35. if (!this.isNumber(data[rule[i].name])) {
  36. this.error = rule[i].errorMsg;
  37. return false;
  38. }
  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 'betweenD':
  48. var reg = /^-?[1-9][0-9]?$/;
  49. if (!reg.test(data[rule[i].name])) {
  50. this.error = rule[i].errorMsg;
  51. return false;
  52. }
  53. var minMax = rule[i].checkRule.split(',');
  54. minMax[0] = Number(minMax[0]);
  55. minMax[1] = Number(minMax[1]);
  56. if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
  57. this.error = rule[i].errorMsg;
  58. return false;
  59. }
  60. break;
  61. case 'betweenF':
  62. var reg = /^-?[0-9][0-9]?.+[0-9]+$/;
  63. if (!reg.test(data[rule[i].name])) {
  64. this.error = rule[i].errorMsg;
  65. return false;
  66. }
  67. var minMax = rule[i].checkRule.split(',');
  68. minMax[0] = Number(minMax[0]);
  69. minMax[1] = Number(minMax[1]);
  70. if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
  71. this.error = rule[i].errorMsg;
  72. return false;
  73. }
  74. break;
  75. case 'same':
  76. if (data[rule[i].name] != rule[i].checkRule) {
  77. this.error = rule[i].errorMsg;
  78. return false;
  79. }
  80. break;
  81. case 'notsame':
  82. if (data[rule[i].name] == rule[i].checkRule) {
  83. this.error = rule[i].errorMsg;
  84. return false;
  85. }
  86. break;
  87. case 'email':
  88. var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  89. if (!reg.test(data[rule[i].name])) {
  90. this.error = rule[i].errorMsg;
  91. return false;
  92. }
  93. break;
  94. case 'bank':
  95. var reg = /^[1-9]\d{9,29}$/;
  96. if (!reg.test(data[rule[i].name])) {
  97. this.error = rule[i].errorMsg;
  98. return false;
  99. }
  100. break;
  101. case 'phoneno':
  102. var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  103. if (data[rule[i].name]) {
  104. if (!reg.test(data[rule[i].name])) {
  105. this.error = rule[i].errorMsg;
  106. return false;
  107. }
  108. }
  109. break;
  110. case 'telephone':
  111. var reg = /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/;
  112. if (data[rule[i].name]) {
  113. if (!reg.test(data[rule[i].name])) {
  114. this.error = rule[i].errorMsg;
  115. return false;
  116. }
  117. }
  118. break;
  119. case 'id':
  120. var reg =
  121. /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
  122. if (!reg.test(data[rule[i].name])) {
  123. this.error = rule[i].errorMsg;
  124. return false;
  125. }
  126. break;
  127. case 'zipcode':
  128. var reg = /^[0-9]{6}$/;
  129. if (!reg.test(data[rule[i].name])) {
  130. this.error = rule[i].errorMsg;
  131. return false;
  132. }
  133. break;
  134. case 'reg':
  135. var reg = new RegExp(rule[i].checkRule);
  136. if (!reg.test(data[rule[i].name])) {
  137. this.error = rule[i].errorMsg;
  138. return false;
  139. }
  140. break;
  141. case 'in':
  142. if (rule[i].checkRule.indexOf(data[rule[i].name]) == -1) {
  143. this.error = rule[i].errorMsg;
  144. return false;
  145. }
  146. break;
  147. case 'notnull':
  148. if (data[rule[i].name] == null || data[rule[i].name].length < 1) {
  149. this.error = rule[i].errorMsg;
  150. return false;
  151. }
  152. break;
  153. case 'editor':
  154. if (data[rule[i].name] == null || data[rule[i].name] === '<p wx:nodeid=\"85\"><br></p>' ||
  155. data[rule[i].name] ===
  156. '<p><br></p>' || data[rule[i].name].length < 1 || data[rule[i].name].length ===
  157. undefined) {
  158. this.error = rule[i].errorMsg;
  159. return false;
  160. }
  161. break;
  162. case 'limit20':
  163. if (data[rule[i].name].length > 20) {
  164. this.error = rule[i].errorMsg;
  165. return false;
  166. }
  167. break;
  168. case 'limit10':
  169. if (data[rule[i].name].length > 10) {
  170. this.error = rule[i].errorMsg;
  171. return false;
  172. }
  173. break;
  174. case 'limit60':
  175. if (data[rule[i].name].length > 60) {
  176. this.error = rule[i].errorMsg;
  177. return false;
  178. }
  179. break;
  180. }
  181. }
  182. return true;
  183. },
  184. isNumber: function(checkVal) {
  185. var reg = /^-?[1-9][0-9]?.?[0-9]*$/;
  186. return reg.test(checkVal);
  187. }
  188. }