formValidation.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * 表单验证
  3. * 来自 ThorUI www.thorui.cn | 文档地址: www.donarui.com
  4. * @author echo.
  5. * @version 1.3.1
  6. **/
  7. const form = {
  8. //当出现错误时返回错误消息,否则返回空即为验证通过
  9. /*
  10. formData:Object 表单对象。{key:value,key:value},key==rules.name
  11. rules: Array [{name:name,rule:[],msg:[]},{name:name,rule:[],msg:[]}]
  12. name:name 属性=> 元素的名称
  13. rule:字符串数组 ["required","isMobile","isEmail","isCarNo","isIdCard","isAmount","isNum","isChinese","isEnglish",isEnAndNo","isSpecial","isEmoji",""isDate","isUrl","isSame:key","range:[1,9]","minLength:9","maxLength:Number"]
  14. msg:数组 []。 与数组 rule 长度相同,对应的错误提示信息
  15. */
  16. validation: function(formData, rules) {
  17. for (let item of rules) {
  18. let key = item.name;
  19. let rule = item.rule;
  20. let msgArr = item.msg;
  21. if (!key || !rule || rule.length === 0 || !msgArr || msgArr.length === 0) {
  22. continue;
  23. }
  24. for (let i = 0, length = rule.length; i < length; i++) {
  25. let ruleItem = rule[i];
  26. let msg = msgArr[i];
  27. if (!ruleItem || !msg) {
  28. continue;
  29. }
  30. //数据处理
  31. let value = null;
  32. if (~ruleItem.indexOf(":")) {
  33. let temp = ruleItem.split(":");
  34. ruleItem = temp[0];
  35. value = temp[1];
  36. }
  37. let isError = false;
  38. switch (ruleItem) {
  39. case "required":
  40. isError = form._isNullOrEmpty(formData[key]);
  41. break;
  42. case "isMobile":
  43. isError = !form._isMobile(formData[key]);
  44. break;
  45. case "isEmail":
  46. isError = !form._isEmail(formData[key]);
  47. break;
  48. case "isCarNo":
  49. isError = !form._isCarNo(formData[key]);
  50. break;
  51. case "isIdCard":
  52. isError = !form._isIdCard(formData[key]);
  53. break;
  54. case "isAmount":
  55. isError = !form._isAmount(formData[key]);
  56. break;
  57. case "isNum":
  58. isError = !form._isNum(formData[key]);
  59. break;
  60. case "isChinese":
  61. isError = !form._isChinese(formData[key]);
  62. break;
  63. case "isEnglish":
  64. isError = !form._isEnglish(formData[key]);
  65. break;
  66. case "isEnAndNo":
  67. isError = !form._isEnAndNo(formData[key]);
  68. break;
  69. case "isSpecial":
  70. isError = !form._isSpecial(formData[key]);
  71. break;
  72. case "isEmoji":
  73. isError = !form._isEmoji(formData[key]);
  74. break;
  75. case "isDate":
  76. isError = !form._isDate(formData[key]);
  77. break;
  78. case "isUrl":
  79. isError = !form._isUrl(formData[key]);
  80. break;
  81. case "isSame":
  82. isError = !form._isSame(formData[key], formData[value]);
  83. break;
  84. case "range":
  85. let range = null;
  86. try {
  87. range = JSON.parse(value);
  88. if (range.length <= 1) {
  89. throw new Error("range值传入有误!")
  90. }
  91. } catch (e) {
  92. return "range值传入有误!"
  93. }
  94. isError = !form._isRange(formData[key], range[0], range[1])
  95. break;
  96. case "minLength":
  97. isError = !form._minLength(formData[key], value)
  98. break;
  99. case "maxLength":
  100. isError = !form._maxLength(formData[key], value)
  101. break;
  102. default:
  103. break;
  104. }
  105. if (isError) {
  106. return msg;
  107. }
  108. }
  109. }
  110. return "";
  111. },
  112. _isNullOrEmpty: function(value) {
  113. return (value === null || value === '' || value === undefined) ? true : false;
  114. },
  115. _isMobile: function(value) {
  116. return /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(value);
  117. },
  118. _isEmail: function(value) {
  119. return /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(value);
  120. },
  121. _isCarNo: function(value) {
  122. // 新能源车牌
  123. const xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
  124. // 旧车牌
  125. const creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
  126. if (value.length === 7) {
  127. return creg.test(value);
  128. } else if (value.length === 8) {
  129. return xreg.test(value);
  130. } else {
  131. return false;
  132. }
  133. },
  134. _isIdCard: function(value) {
  135. let idCard = value;
  136. if (idCard.length == 15) {
  137. return this.__isValidityBrithBy15IdCard;
  138. } else if (idCard.length == 18) {
  139. let arrIdCard = idCard.split("");
  140. if (this.__isValidityBrithBy18IdCard(idCard) && this.__isTrueValidateCodeBy18IdCard(arrIdCard)) {
  141. return true;
  142. } else {
  143. return false;
  144. }
  145. } else {
  146. return false;
  147. }
  148. },
  149. __isTrueValidateCodeBy18IdCard: function(arrIdCard) {
  150. let sum = 0;
  151. let Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
  152. let ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];
  153. if (arrIdCard[17].toLowerCase() == 'x') {
  154. arrIdCard[17] = 10;
  155. }
  156. for (let i = 0; i < 17; i++) {
  157. sum += Wi[i] * arrIdCard[i];
  158. }
  159. let valCodePosition = sum % 11;
  160. if (arrIdCard[17] == ValideCode[valCodePosition]) {
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. },
  166. __isValidityBrithBy18IdCard: function(idCard18) {
  167. let year = idCard18.substring(6, 10);
  168. let month = idCard18.substring(10, 12);
  169. let day = idCard18.substring(12, 14);
  170. let temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
  171. if (temp_date.getFullYear() != parseFloat(year) || temp_date.getMonth() != parseFloat(month) - 1 || temp_date.getDate() !=
  172. parseFloat(day)) {
  173. return false;
  174. } else {
  175. return true;
  176. }
  177. },
  178. __isValidityBrithBy15IdCard: function(idCard15) {
  179. let year = idCard15.substring(6, 8);
  180. let month = idCard15.substring(8, 10);
  181. let day = idCard15.substring(10, 12);
  182. let temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
  183. if (temp_date.getYear() != parseFloat(year) || temp_date.getMonth() != parseFloat(month) - 1 || temp_date.getDate() !=
  184. parseFloat(day)) {
  185. return false;
  186. } else {
  187. return true;
  188. }
  189. },
  190. _isAmount: function(value) {
  191. //金额,只允许保留两位小数
  192. return /^([0-9]*[.]?[0-9])[0-9]{0,1}$/.test(value);
  193. },
  194. _isNum: function(value) {
  195. //只能为数字
  196. return /^[0-9]+$/.test(value);
  197. },
  198. _isChinese: function(value) {
  199. let reg = /^[\u0391-\uFFE5]+$/;
  200. return value !== "" && reg.test(value) && !form._isSpecial(value) && !form._isEmoji(value)
  201. },
  202. _isEnglish: function(value) {
  203. return /^[a-zA-Z]*$/.test(value)
  204. },
  205. _isEnAndNo: function(value) {
  206. //8~20位数字和字母组合 按需修改
  207. return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/.test(value);
  208. },
  209. _isSpecial: function(value) {
  210. //是否包含特殊字符
  211. let regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im,
  212. regCn = /[·!#¥(——):;“”‘、,|《。》?、【】[\]]/im;
  213. if (regEn.test(value) || regCn.test(value)) {
  214. return true;
  215. }
  216. return false;
  217. },
  218. _isEmoji: function(value) {
  219. //是否包含表情
  220. return /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g.test(value);
  221. },
  222. _isDate: function(value) {
  223. //2019-10-12
  224. const reg =
  225. /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
  226. return reg.test(value);
  227. },
  228. _isUrl: function(value) {
  229. return /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test(value);
  230. },
  231. _isSame: function(value1, value2) {
  232. return value1 === value2
  233. },
  234. _isRange: function(value, range1, range2) {
  235. if ((!range1 && range1 != 0) && (!range2 && range2 != 0)) {
  236. return true;
  237. } else if (!range1 && range1 != 0) {
  238. return value <= range2
  239. } else if (!range2 && range2 != 0) {
  240. return value >= range1
  241. } else {
  242. return value >= range1 && value <= range2
  243. }
  244. },
  245. _minLength: function(value, min) {
  246. console.log(value)
  247. console.log(min)
  248. return value.length >= Number(min)
  249. },
  250. _maxLength: function(value, max) {
  251. return value.length <= Number(max)
  252. }
  253. };
  254. module.exports = {
  255. validation: form.validation
  256. };