tui-validation.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * 表单验证
  3. * @author echo.
  4. * @version 1.6.6
  5. **/
  6. const form = {
  7. //非必填情况下,如果值为空,则不进行校验
  8. //当出现错误时返回错误消息,否则返回空即为验证通过
  9. /*
  10. formData:Object 表单对象。{key:value,key:value},key==rules.name
  11. rules: Array [{name:name,rule:[],msg:[],validator:[],{name:name,rule:[],msg:[],validator:[]}]
  12. name:name 属性=> 元素的名称
  13. rule:字符串数组 ["required","isMobile","isEmail","isCarNo","isIdCard","isAmount","isNum","isChinese","isNotChinese","isEnglish",isEnAndNo","isSpecial","isEmoji",""isDate","isUrl","isSame:key","range:[1,9]","minLength:9","maxLength:Number","isKeyword:key1,key2,key3..."]
  14. msg:数组 []。 与数组 rule 长度相同,对应的错误提示信息
  15. validator:[{msg:'错误消息',method:Function}],自定义验证方法组,函数约定:(value)=>{ return true or false}
  16. */
  17. validation: function(formData, rules) {
  18. for (let item of rules) {
  19. let key = item.name;
  20. let rule = item.rule;
  21. let validator = item.validator;
  22. let msgArr = item.msg;
  23. if (!key || !rule || rule.length === 0 || !msgArr || msgArr.length === 0 || (!~rule.indexOf(
  24. "required") && formData[key].toString()
  25. .length === 0)) {
  26. continue;
  27. }
  28. for (let i = 0, length = rule.length; i < length; i++) {
  29. let ruleItem = rule[i];
  30. let msg = msgArr[i];
  31. if (!msg || !ruleItem) continue;
  32. //数据处理
  33. let value = null;
  34. if (~ruleItem.indexOf(":")) {
  35. let temp = ruleItem.split(":");
  36. ruleItem = temp[0];
  37. value = temp[1];
  38. }
  39. let isError = false;
  40. switch (ruleItem) {
  41. case "required":
  42. isError = form._isNullOrEmpty(formData[key]);
  43. break;
  44. case "isMobile":
  45. isError = !form._isMobile(formData[key]);
  46. break;
  47. case "isEmail":
  48. isError = !form._isEmail(formData[key]);
  49. break;
  50. case "isCarNo":
  51. isError = !form._isCarNo(formData[key]);
  52. break;
  53. case "isIdCard":
  54. isError = !form._isIdCard(formData[key]);
  55. break;
  56. case "isAmount":
  57. isError = !form._isAmount(formData[key]);
  58. break;
  59. case "isNum":
  60. isError = !form._isNum(formData[key]);
  61. break;
  62. case "isChinese":
  63. isError = !form._isChinese(formData[key]);
  64. break;
  65. case "isNotChinese":
  66. isError = !form._isNotChinese(formData[key]);
  67. break;
  68. case "isEnglish":
  69. isError = !form._isEnglish(formData[key]);
  70. break;
  71. case "isEnAndNo":
  72. isError = !form._isEnAndNo(formData[key]);
  73. break;
  74. case "isEnOrNo":
  75. isError = !form._isEnOrNo(formData[key]);
  76. break;
  77. case "isSpecial":
  78. isError = form._isSpecial(formData[key]);
  79. break;
  80. case "isEmoji":
  81. isError = form._isEmoji(formData[key]);
  82. break;
  83. case "isDate":
  84. isError = !form._isDate(formData[key]);
  85. break;
  86. case "isUrl":
  87. isError = !form._isUrl(formData[key]);
  88. break;
  89. case "isSame":
  90. isError = !form._isSame(formData[key], formData[value]);
  91. break;
  92. case "range":
  93. let range = null;
  94. try {
  95. range = JSON.parse(value);
  96. if (range.length <= 1) {
  97. throw new Error("range值传入有误!")
  98. }
  99. } catch (e) {
  100. return "range值传入有误!"
  101. }
  102. isError = !form._isRange(formData[key], range[0], range[1])
  103. break;
  104. case "minLength":
  105. isError = !form._minLength(formData[key], value)
  106. break;
  107. case "maxLength":
  108. isError = !form._maxLength(formData[key], value)
  109. break;
  110. case "isKeyword":
  111. isError = !form._isKeyword(formData[key], value)
  112. break;
  113. default:
  114. break;
  115. }
  116. if (isError) {
  117. return msg;
  118. }
  119. }
  120. if (validator && validator.length > 0) {
  121. for (let model of validator) {
  122. let func = model.method;
  123. if (func && !func(formData[key])) {
  124. return model.msg;
  125. }
  126. }
  127. }
  128. }
  129. return "";
  130. },
  131. //允许填写字符串null或者undefined
  132. _isNullOrEmpty: function(value) {
  133. return (value === null || value === '' || value === undefined) ? true : false;
  134. },
  135. _isMobile: function(value) {
  136. return /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(value);
  137. },
  138. _isEmail: function(value) {
  139. return /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(value);
  140. },
  141. _isCarNo: function(value) {
  142. // 新能源车牌
  143. const xreg =
  144. /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
  145. // 旧车牌
  146. const creg =
  147. /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
  148. if (value.length === 7) {
  149. return creg.test(value);
  150. } else if (value.length === 8) {
  151. return xreg.test(value);
  152. } else {
  153. return false;
  154. }
  155. },
  156. _isIdCard: function(value) {
  157. let idCard = value;
  158. if (idCard.length == 15) {
  159. return this.__isValidityBrithBy15IdCard;
  160. } else if (idCard.length == 18) {
  161. let arrIdCard = idCard.split("");
  162. if (this.__isValidityBrithBy18IdCard(idCard) && this.__isTrueValidateCodeBy18IdCard(arrIdCard)) {
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. } else {
  168. return false;
  169. }
  170. },
  171. __isTrueValidateCodeBy18IdCard: function(arrIdCard) {
  172. let sum = 0;
  173. let Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
  174. let ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];
  175. if (arrIdCard[17].toLowerCase() == 'x') {
  176. arrIdCard[17] = 10;
  177. }
  178. for (let i = 0; i < 17; i++) {
  179. sum += Wi[i] * arrIdCard[i];
  180. }
  181. let valCodePosition = sum % 11;
  182. if (arrIdCard[17] == ValideCode[valCodePosition]) {
  183. return true;
  184. } else {
  185. return false;
  186. }
  187. },
  188. __isValidityBrithBy18IdCard: function(idCard18) {
  189. let year = idCard18.substring(6, 10);
  190. let month = idCard18.substring(10, 12);
  191. let day = idCard18.substring(12, 14);
  192. let temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
  193. if (temp_date.getFullYear() != parseFloat(year) || temp_date.getMonth() != parseFloat(month) - 1 ||
  194. temp_date.getDate() !=
  195. parseFloat(day)) {
  196. return false;
  197. } else {
  198. return true;
  199. }
  200. },
  201. __isValidityBrithBy15IdCard: function(idCard15) {
  202. let year = idCard15.substring(6, 8);
  203. let month = idCard15.substring(8, 10);
  204. let day = idCard15.substring(10, 12);
  205. let temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
  206. if (temp_date.getYear() != parseFloat(year) || temp_date.getMonth() != parseFloat(month) - 1 ||
  207. temp_date.getDate() !=
  208. parseFloat(day)) {
  209. return false;
  210. } else {
  211. return true;
  212. }
  213. },
  214. _isAmount: function(value) {
  215. //金额,只允许保留两位小数
  216. return /^([0-9]*[.]?[0-9])[0-9]{0,1}$/.test(value);
  217. },
  218. _isNum: function(value) {
  219. //只能为数字
  220. return /^[0-9]+$/.test(value);
  221. },
  222. //是否全部为中文
  223. _isChinese: function(value) {
  224. let reg = /^[\u4e00-\u9fa5]+$/;
  225. return value !== "" && reg.test(value) && !form._isSpecial(value) && !form._isEmoji(value)
  226. },
  227. //是否不包含中文,可以有特殊字符
  228. _isNotChinese: function(value) {
  229. let reg = /.*[\u4e00-\u9fa5]+.*$/;
  230. let result = true;
  231. if (reg.test(value)) {
  232. result = false
  233. }
  234. return result
  235. },
  236. _isEnglish: function(value) {
  237. return /^[a-zA-Z]*$/.test(value)
  238. },
  239. _isEnAndNo: function(value) {
  240. //8~20位数字和字母组合
  241. return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/.test(value);
  242. },
  243. _isEnOrNo: function(value) {
  244. //英文或者数字
  245. let reg = /.*[\u4e00-\u9fa5]+.*$/;
  246. let result = true;
  247. if (reg.test(value) || form._isSpecial(value) || form._isEmoji(value)) {
  248. result = false
  249. }
  250. return result
  251. },
  252. _isSpecial: function(value) {
  253. //是否包含特殊字符
  254. let regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im,
  255. regCn = /[·!#¥(——):;“”‘、,|《。》?、【】[\]]/im;
  256. if (regEn.test(value) || regCn.test(value)) {
  257. return true;
  258. }
  259. return false;
  260. },
  261. _isEmoji: function(value) {
  262. //是否包含表情
  263. return /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g.test(value);
  264. },
  265. _isDate: function(value) {
  266. //2019-10-12
  267. const reg =
  268. /^(?:(?!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)$/;
  269. return reg.test(value);
  270. },
  271. _isUrl: function(value) {
  272. return /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})(:[0-9]{1,5})?((\/?)|(\/[\\\w_!~*\\'()\\\.;?:@&=+$,%#-]+)+\/?)$/.test(value);
  273. },
  274. _isSame: function(value1, value2) {
  275. return value1 === value2
  276. },
  277. _isRange: function(value, range1, range2) {
  278. if ((!range1 && range1 != 0) && (!range2 && range2 != 0)) {
  279. return true;
  280. } else if (!range1 && range1 != 0) {
  281. return value <= range2
  282. } else if (!range2 && range2 != 0) {
  283. return value >= range1
  284. } else {
  285. return value >= range1 && value <= range2
  286. }
  287. },
  288. _minLength: function(value, min) {
  289. return value.length >= Number(min)
  290. },
  291. _maxLength: function(value, max) {
  292. return value.length <= Number(max)
  293. },
  294. _isKeyword: function(value, keywords) {
  295. //是否包含关键词,敏感词,多个以英文逗号分隔,包含则为false,弹出提示语!
  296. let result = true;
  297. if (!keywords) return result;
  298. let key = keywords.split(',');
  299. for (let i = 0, len = key.length; i < len; i++) {
  300. if (~value.indexOf(key[i])) {
  301. result = false;
  302. break;
  303. }
  304. }
  305. return result;
  306. }
  307. };
  308. export default {
  309. validation: form.validation
  310. };