checker.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import idCardChecker from './checkIdCard.js';
  2. module.exports = {
  3. error : '',
  4. check : function (dataBeCheck, rule){
  5. dataBeCheck = JSON.stringify(dataBeCheck);
  6. var data = JSON.parse(dataBeCheck);
  7. for(var i = 0; i < rule.length; i++){
  8. if (!rule[i].checkType){ return true;}
  9. if (!rule[i].name){return true;}
  10. if (!rule[i].errorMsg) {return true;}
  11. if (
  12. typeof(data[rule[i].name]) == 'undefined'
  13. ||
  14. data[rule[i].name] === ''
  15. ){
  16. this.error = rule[i].errorMsg;
  17. return false;
  18. }
  19. // 检查前去除内容的空格及换行
  20. if(typeof(data[rule[i].name]) == 'string'){
  21. data[rule[i].name] = data[rule[i].name].replace(/\s/g,"");
  22. }
  23. switch (rule[i].checkType){
  24. case 'string':
  25. var reg = new RegExp('^.{' + rule[i].checkRule + '}$');
  26. if(!reg.test(data[rule[i].name])) {
  27. this.error = rule[i].errorMsg; return false;
  28. }
  29. break;
  30. case 'contain':
  31. var cData = data[rule[i].name]+'';
  32. if(cData.indexOf(rule[i].checkRule) == -1){
  33. this.error = rule[i].errorMsg;
  34. return false;
  35. }
  36. break;
  37. case 'notContain':
  38. var cData = data[rule[i].name]+'';
  39. if(cData.indexOf(rule[i].checkRule) != -1){
  40. this.error = rule[i].errorMsg;
  41. return false;
  42. }
  43. break;
  44. case 'inArray':
  45. if(typeof(rule[i].checkRule) != 'object'){
  46. this.error = rule[i].errorMsg;
  47. return false;
  48. }
  49. var resInArray = rule[i].checkRule.find(
  50. (val)=>{
  51. if(val == data[rule[i].name]){
  52. return true;
  53. }
  54. }
  55. );
  56. if(!resInArray){
  57. this.error = rule[i].errorMsg;
  58. return false;
  59. }
  60. break;
  61. case 'notInArray':
  62. if(typeof(rule[i].checkRule) != 'object'){
  63. this.error = rule[i].errorMsg;
  64. return false;
  65. }
  66. var resInArray = rule[i].checkRule.find(
  67. (val)=>{
  68. if(val == data[rule[i].name]){
  69. return true;
  70. }
  71. }
  72. );
  73. if(resInArray){
  74. this.error = rule[i].errorMsg;
  75. return false;
  76. }
  77. break;
  78. case 'int':
  79. var ruleArr = rule[i].checkRule.split(',');
  80. if(rule.length < 2){
  81. ruleArr[0] = Number(ruleArr[0]) - 1;
  82. ruleArr[1] = '';
  83. }else{
  84. ruleArr[0] = Number(ruleArr[0]) - 1;
  85. ruleArr[1] = Number(ruleArr[1]) - 1;
  86. }
  87. var reg = new RegExp('^-?\\d{' + ruleArr[0] + ',' + ruleArr[1] + '}$');
  88. if(!reg.test(data[rule[i].name])) {
  89. this.error = rule[i].errorMsg;
  90. return false;
  91. }
  92. break;
  93. break;
  94. case 'between':
  95. if (!this.isNumber(data[rule[i].name])){
  96. this.error = rule[i].errorMsg;
  97. return false;
  98. }
  99. var minMax = rule[i].checkRule.split(',');
  100. minMax[0] = Number(minMax[0]);
  101. minMax[1] = Number(minMax[1]);
  102. if (
  103. data[rule[i].name] > minMax[1]
  104. ||
  105. data[rule[i].name] < minMax[0])
  106. {
  107. this.error = rule[i].errorMsg;
  108. return false;
  109. }
  110. break;
  111. case 'intBetween':
  112. var reg = /^-?\d+$/;
  113. if (!reg.test(data[rule[i].name])) {
  114. this.error = rule[i].errorMsg;
  115. return false;
  116. }
  117. var minMax = rule[i].checkRule.split(',');
  118. minMax[0] = Number(minMax[0]);
  119. minMax[1] = Number(minMax[1]);
  120. if (
  121. data[rule[i].name] > minMax[1]
  122. ||
  123. data[rule[i].name] < minMax[0]) {
  124. this.error = rule[i].errorMsg;
  125. return false;
  126. }
  127. break;
  128. case 'betweenD':
  129. var reg = /^-?\d+$/;
  130. if (!reg.test(data[rule[i].name])){
  131. this.error = rule[i].errorMsg;
  132. return false;
  133. }
  134. var minMax = rule[i].checkRule.split(',');
  135. minMax[0] = Number(minMax[0]);
  136. minMax[1] = Number(minMax[1]);
  137. if (
  138. data[rule[i].name] > minMax[1]
  139. ||
  140. data[rule[i].name] < minMax[0]){
  141. this.error = rule[i].errorMsg;
  142. return false;
  143. }
  144. break;
  145. case 'doubleBetween':
  146. var reg = /^-?\d?.+\d+$/;
  147. if (!reg.test(data[rule[i].name])){
  148. this.error = rule[i].errorMsg;
  149. return false;
  150. }
  151. var minMax = rule[i].checkRule.split(',');
  152. minMax[0] = Number(minMax[0]);
  153. minMax[1] = Number(minMax[1]);
  154. if (
  155. data[rule[i].name] > minMax[1]
  156. ||
  157. data[rule[i].name] < minMax[0]){
  158. this.error = rule[i].errorMsg;
  159. return false;
  160. }
  161. break;
  162. case 'betweenF':
  163. var reg = /^-?\d?.+\d+$/;
  164. if (!reg.test(data[rule[i].name])){
  165. this.error = rule[i].errorMsg;
  166. return false;
  167. }
  168. var minMax = rule[i].checkRule.split(',');
  169. minMax[0] = Number(minMax[0]);
  170. minMax[1] = Number(minMax[1]);
  171. if (
  172. data[rule[i].name] > minMax[1]
  173. ||
  174. data[rule[i].name] < minMax[0]) {
  175. this.error = rule[i].errorMsg;
  176. return false;
  177. }
  178. break;
  179. case 'doubleLength' :
  180. var reg = new RegExp('^-?\\d+.\\d{' + rule[i].checkRule + '}$');
  181. if (!reg.test(data[rule[i].name])){
  182. this.error = rule[i].errorMsg;
  183. return false;
  184. }
  185. break;
  186. case 'gt':
  187. if(data[rule[i].name] <= rule[i].checkRule){
  188. this.error = rule[i].errorMsg;
  189. return false;
  190. }
  191. break;
  192. case 'gtAndSame':
  193. if(data[rule[i].name] < rule[i].checkRule){
  194. this.error = rule[i].errorMsg;
  195. return false;
  196. }
  197. break;
  198. case 'lt':
  199. if(data[rule[i].name] >= rule[i].checkRule){
  200. this.error = rule[i].errorMsg;
  201. return false;
  202. }
  203. break;
  204. case 'ltAndSame':
  205. if(data[rule[i].name] > rule[i].checkRule){
  206. this.error = rule[i].errorMsg;
  207. return false;
  208. }
  209. break;
  210. case 'same':
  211. if(data[rule[i].name] != rule[i].checkRule){
  212. this.error = rule[i].errorMsg;
  213. return false;
  214. }
  215. break;
  216. case 'notSame':
  217. if(data[rule[i].name] == rule[i].checkRule){
  218. this.error = rule[i].errorMsg; return false;
  219. }
  220. break;
  221. case 'notsame':
  222. if(data[rule[i].name] == rule[i].checkRule){
  223. this.error = rule[i].errorMsg; return false;
  224. }
  225. break;
  226. case 'email':
  227. var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  228. if (!reg.test(data[rule[i].name])){
  229. this.error = rule[i].errorMsg;
  230. return false;
  231. }
  232. break;
  233. case 'phoneno':
  234. var reg = /^1[0-9]{10,10}$/;
  235. if (!reg.test(data[rule[i].name])){
  236. this.error = rule[i].errorMsg;
  237. return false;
  238. }
  239. break;
  240. case 'phone':
  241. var reg = /^1[0-9]{10,10}$/;
  242. if (!reg.test(data[rule[i].name])){
  243. this.error = rule[i].errorMsg;
  244. return false;
  245. }
  246. break;
  247. case 'zipcode':
  248. var reg = /^[0-9]{6}$/;
  249. if (!reg.test(data[rule[i].name])){
  250. this.error = rule[i].errorMsg;
  251. return false;
  252. }
  253. break;
  254. case 'reg':
  255. var reg = new RegExp(rule[i].checkRule);
  256. if (!reg.test(data[rule[i].name])) {
  257. this.error = rule[i].errorMsg;
  258. return false;
  259. }
  260. break;
  261. case 'in':
  262. if(rule[i].checkRule.indexOf(data[rule[i].name]) == -1){
  263. this.error = rule[i].errorMsg; return false;
  264. }
  265. break;
  266. case 'notnull':
  267. if(
  268. data[rule[i].name] == null || data[rule[i].name].length < 1
  269. ){this.error = rule[i].errorMsg; return false;}
  270. break;
  271. case 'samewith':
  272. if(data[rule[i].name] != data[rule[i].checkRule]){
  273. this.error = rule[i].errorMsg;
  274. return false;
  275. }
  276. break;
  277. case 'numbers':
  278. var reg = new RegExp('^[0-9]{' + rule[i].checkRule + '}$');
  279. if (!reg.test(data[rule[i].name])){
  280. this.error = rule[i].errorMsg; return false;
  281. }
  282. break;
  283. case 'url':
  284. var reg = /^(\w+:\/\/)?\w+(\.\w+)+.*$/;
  285. if (!reg.test(data[rule[i].name])){
  286. this.error = rule[i].errorMsg;
  287. return false;
  288. }
  289. break;
  290. case 'idCard' :
  291. var idCardRes = idCardChecker.checkIdcard(data[rule[i].name]);
  292. if(idCardRes != 'ok'){
  293. this.error = idCardRes;
  294. return false;
  295. }
  296. break;
  297. }
  298. }
  299. return true;
  300. },
  301. isNumber : function (checkVal){
  302. checkVal = Number(checkVal);
  303. if(checkVal == NaN){return false;}
  304. return true;
  305. }
  306. }