graceChecker.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 'phoneno':
  95. var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  96. if (data[rule[i].name]) {
  97. if (!reg.test(data[rule[i].name])) {
  98. this.error = rule[i].errorMsg;
  99. return false;
  100. }
  101. }
  102. break;
  103. case 'telephone':
  104. var reg = /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/;
  105. if (data[rule[i].name]) {
  106. if (!reg.test(data[rule[i].name])) {
  107. this.error = rule[i].errorMsg;
  108. return false;
  109. }
  110. }
  111. break;
  112. case 'id':
  113. var reg =
  114. /^[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)$/;
  115. if (!reg.test(data[rule[i].name])) {
  116. this.error = rule[i].errorMsg;
  117. return false;
  118. }
  119. break;
  120. case 'zipcode':
  121. var reg = /^[0-9]{6}$/;
  122. if (!reg.test(data[rule[i].name])) {
  123. this.error = rule[i].errorMsg;
  124. return false;
  125. }
  126. break;
  127. case 'reg':
  128. var reg = new RegExp(rule[i].checkRule);
  129. if (!reg.test(data[rule[i].name])) {
  130. this.error = rule[i].errorMsg;
  131. return false;
  132. }
  133. break;
  134. case 'in':
  135. if (rule[i].checkRule.indexOf(data[rule[i].name]) == -1) {
  136. this.error = rule[i].errorMsg;
  137. return false;
  138. }
  139. break;
  140. case 'notnull':
  141. if (data[rule[i].name] == null || data[rule[i].name].length < 1) {
  142. this.error = rule[i].errorMsg;
  143. return false;
  144. }
  145. break;
  146. case 'editor':
  147. if (data[rule[i].name] == null || data[rule[i].name] === '<p wx:nodeid=\"85\"><br></p>' ||
  148. data[rule[i].name] ===
  149. '<p><br></p>' || data[rule[i].name].length < 1 || data[rule[i].name].length ===
  150. undefined) {
  151. this.error = rule[i].errorMsg;
  152. return false;
  153. }
  154. break;
  155. case 'limit20':
  156. if (data[rule[i].name].length > 20) {
  157. this.error = rule[i].errorMsg;
  158. return false;
  159. }
  160. break;
  161. case 'limit10':
  162. if (data[rule[i].name].length > 10) {
  163. this.error = rule[i].errorMsg;
  164. return false;
  165. }
  166. break;
  167. case 'limit60':
  168. if (data[rule[i].name].length > 60) {
  169. this.error = rule[i].errorMsg;
  170. return false;
  171. }
  172. break;
  173. }
  174. }
  175. return true;
  176. },
  177. isNumber: function(checkVal) {
  178. var reg = /^-?[1-9][0-9]?.?[0-9]*$/;
  179. return reg.test(checkVal);
  180. }
  181. }