tui-validation.min.js
4.3 KB
const form={validation:function(a,b){for(let c of b){let b=c.name,d=c.rule,e=c.msg;if(b&&d&&0!==d.length&&e&&0!==e.length)for(let c=0,f=d.length;c<f;c++){let f=d[c],g=e[c];if(!f||!g||!~d.indexOf("required")&&0===a[b].toString().length)continue;let h=null;if(~f.indexOf(":")){let a=f.split(":");f=a[0],h=a[1]}let i=!1;switch(f){case"required":i=form._isNullOrEmpty(a[b]);break;case"isMobile":i=!form._isMobile(a[b]);break;case"isEmail":i=!form._isEmail(a[b]);break;case"isCarNo":i=!form._isCarNo(a[b]);break;case"isIdCard":i=!form._isIdCard(a[b]);break;case"isAmount":i=!form._isAmount(a[b]);break;case"isNum":i=!form._isNum(a[b]);break;case"isChinese":i=!form._isChinese(a[b]);break;case"isEnglish":i=!form._isEnglish(a[b]);break;case"isEnAndNo":i=!form._isEnAndNo(a[b]);break;case"isEnOrNo":i=!form._isEnOrNo(a[b]);break;case"isSpecial":i=form._isSpecial(a[b]);break;case"isEmoji":i=form._isEmoji(a[b]);break;case"isDate":i=!form._isDate(a[b]);break;case"isUrl":i=!form._isUrl(a[b]);break;case"isSame":i=!form._isSame(a[b],a[h]);break;case"range":let c=null;try{if(c=JSON.parse(h),1>=c.length)throw new Error("range\u503C\u4F20\u5165\u6709\u8BEF\uFF01")}catch(a){return"range\u503C\u4F20\u5165\u6709\u8BEF\uFF01"}i=!form._isRange(a[b],c[0],c[1]);break;case"minLength":i=!form._minLength(a[b],h);break;case"maxLength":i=!form._maxLength(a[b],h);break;default:}if(i)return g}}return""},_isNullOrEmpty:function(a){return!(null!==a&&""!==a&&a!==void 0)},_isMobile:function(a){return /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(a)},_isEmail:function(a){return /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(a)},_isCarNo:function(a){const b=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/,c=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;return 7===a.length?c.test(a):8===a.length&&b.test(a)},_isIdCard:function(a){let b=a;if(15==b.length)return this.__isValidityBrithBy15IdCard;if(18==b.length){let a=b.split("");return!!(this.__isValidityBrithBy18IdCard(b)&&this.__isTrueValidateCodeBy18IdCard(a))}return!1},__isTrueValidateCodeBy18IdCard:function(a){let b=0,c=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1];"x"==a[17].toLowerCase()&&(a[17]=10);for(let d=0;17>d;d++)b+=c[d]*a[d];let d=b%11;return!(a[17]!=[1,0,10,9,8,7,6,5,4,3,2][d])},__isValidityBrithBy18IdCard:function(a){let b=a.substring(6,10),c=a.substring(10,12),d=a.substring(12,14),e=new Date(b,parseFloat(c)-1,parseFloat(d));return e.getFullYear()==parseFloat(b)&&e.getMonth()==parseFloat(c)-1&&e.getDate()==parseFloat(d)},__isValidityBrithBy15IdCard:function(a){let b=a.substring(6,8),c=a.substring(8,10),d=a.substring(10,12),e=new Date(b,parseFloat(c)-1,parseFloat(d));return e.getYear()==parseFloat(b)&&e.getMonth()==parseFloat(c)-1&&e.getDate()==parseFloat(d)},_isAmount:function(a){return /^([0-9]*[.]?[0-9])[0-9]{0,1}$/.test(a)},_isNum:function(a){return /^[0-9]+$/.test(a)},_isChinese:function(a){let b=/.*[\u4e00-\u9fa5]+.*$/;return""!==a&&b.test(a)&&!form._isSpecial(a)&&!form._isEmoji(a)},_isEnglish:function(a){return /^[a-zA-Z]*$/.test(a)},_isEnAndNo:function(a){return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/.test(a)},_isEnOrNo:function(a){let b=/.*[\u4e00-\u9fa5]+.*$/,c=!0;return(b.test(a)||form._isSpecial(a)||form._isEmoji(a))&&(c=!1),c},_isSpecial:function(a){let b=/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im,c=/[·!#¥(——):;“”‘、,|《。》?、【】[\]]/im;return!!(b.test(a)||c.test(a))},_isEmoji:function(a){return /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g.test(a)},_isDate:function(a){const b=/^(?:(?!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)$/;return b.test(a)},_isUrl:function(a){return /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test(a)},_isSame:function(a,b){return a===b},_isRange:function(a,b,c){return!(b||0==b||c||0==c)||(b||0==b?c||0==c?a>=b&&a<=c:a>=b:a<=c)},_minLength:function(a,b){return a.length>=+b},_maxLength:function(a,b){return a.length<=+b}};module.exports={validation:form.validation};