FormValidation

表单验证,只需配置验证项以及相关提示

组件脚本

  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. thrownewError("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. }elseif(value.length ===8){
  129. return xreg.test(value);
  130. }else{
  131. returnfalse;
  132. }
  133. },
  134. _isIdCard:function(value){
  135. let idCard = value;
  136. if(idCard.length ==15){
  137. returnthis.__isValidityBrithBy15IdCard;
  138. }elseif(idCard.length ==18){
  139. let arrIdCard = idCard.split("");
  140. if(this.__isTrueValidateCodeBy18IdCard(idCard)&&this.__isTrueValidateCodeBy18IdCard(arrIdCard)){
  141. returntrue;
  142. }else{
  143. returnfalse;
  144. }
  145. }else{
  146. returnfalse;
  147. }
  148. },
  149. __isTrueValidateCodeBy18IdCard:function(arrIdCard){
  150. let sum =0;
  151. letWi=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1];
  152. letValideCode=[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. returntrue;
  162. }else{
  163. returnfalse;
  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 =newDate(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. returnfalse;
  174. }else{
  175. returntrue;
  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 =newDate(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. returnfalse;
  186. }else{
  187. returntrue;
  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. returntrue;
  215. }
  216. returnfalse;
  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. returntrue;
  237. }elseif(!range1 && range1 !=0){
  238. return value <= range2
  239. }elseif(!range2 && range2 !=0){
  240. return value >= range1
  241. }else{
  242. return value >= range1 && value <= range2
  243. }
  244. },
  245. _minLength:function(value, min){
  246. return value.length >=Number(min)
  247. },
  248. _maxLength:function(value, max){
  249. return value.length <=Number(max)
  250. }
  251. };
  252. module.exports ={
  253. validation: form.validation
  254. };

脚本说明

  1. methods:
  2. "validation":表单验证唯一方法,传入相应参数
  3. "validation"参数:"formData":Object表单对象。参数格式:{key:value,key:value}, key===rules.name
  4. "validation"参数:"rules":Array参数格式:[{name:name,rule:[],msg:[]},{name:name,rule:[],msg:[]}]
  5. rules属性描述
  6. "name":name 属性=>元素的名称
  7. "rule":字符串数组
  8. [
  9. "required",//=>必填
  10. "isMobile",//=>是否为手机号
  11. "isEmail",//=>是否为邮箱
  12. "isCarNo",//=>是否为车牌号
  13. "isIdCard",//=>是否为身份证号
  14. "isAmount",//=>是否为金额,允许保留两位小数
  15. "isNum",//=>是否为数字
  16. "isChinese",//=>是否为中文
  17. "isEnglish",//=>是否为英文
  18. "isEnAndNo",//=>是否为英文和数字组合 8~20位
  19. "isSpecial",//=>是否有特殊字符
  20. "isEmoji",//=>是否有表情符号
  21. "isDate",//=>是否为日期
  22. "isUrl",//=>是否为地址
  23. "isSame:key",//=>一般校验两次输入的值是否相同
  24. "range:[1,9]",//=>数值范围
  25. "minLength:9",//=>最小长度
  26. "maxLength:Number"//=>最大长度
  27. ]
  28. "msg":数组[]。与数组 rule 长度相同,对应的错误提示信息

示例

  1. ...此处省略n行,下载源码查看

scroll-top