/*! * cxValidation * @name cxvalidation.js * @version 0.8.1 * @date 2017-09-18 * @author ciaoca * @email ciaoca@gmail.com * @site https://github.com/ciaoca/cxValidation * @license Released under the MIT license */ !function(a,b){"use strict";var d,c={defaults:{complete:b,success:b,error:b},result:{status:!0},validMessage:{required:"",groupRequired:"至少填写 {{1}} 项",condRequired:"",equals:"两次输入的不一致",minSize:"最少 {{0}} 个字符",maxSize:"最多 {{0}} 个字符",min:"最小值为 {{0}}",max:"最大值为 {{0}}",integer:"无效的整数",number:"无效的数值",onlyNumber:"只能填写数字",onlyNumberSp:"只能填写数字和空格",onlyLetter:"只能填写英文字母",onlyLetterSp:"只能填写英文字母和空格",onlyLetterNumber:"只能填写英文字母与数字",onlyLetterNumberSp:"只能填写英文字母、数字、空格",email:"无效的邮件地址",phone:"无效的电话号码",url:"无效的网址",chinese:"只能填写中文汉字",chinaId:"无效的身份证号码",chinaIdLoose:"无效的身份证号码",chinaZip:"无效的邮政编码",qq:"无效的 QQ 号码"},isElement:function(a){return a&&("function"==typeof HTMLElement||"object"==typeof HTMLElement)&&a instanceof HTMLElement?!0:a&&a.nodeType&&1===a.nodeType?!0:!1},isJquery:function(a){return a&&a.length&&("function"==typeof jQuery||"object"==typeof jQuery)&&a instanceof jQuery?!0:!1},isZepto:function(a){return a&&a.length&&("function"==typeof Zepto||"object"==typeof Zepto)&&Zepto.zepto.isZ(a)?!0:!1},isHidden:function(b){if(this.isElement(b)){var c=a.getComputedStyle(b);return"none"===c.getPropertyValue("display")||"hidden"===c.getPropertyValue("visibility")||0==c.getPropertyValue("opacity")||0==c.getPropertyValue("width")&&0==c.getPropertyValue("height")?!0:!1}return!0},isVisible:function(a){return!this.isHidden(a)},isAndroid:/android/i.test(navigator.userAgent)};c.validFun={required:function(a){return"checkbox"===a.type||"radio"===a.type?a.checked?!0:!1:a.value.trim().length?!0:!1},groupRequired:function(a,b,d){return b=String(b),d=parseInt(d,10),isNaN(d)&&(d=1),("undefined"==typeof c.groupCache[b]||"undefined"==typeof c.groupCache[b].count)&&(c.groupCache[b]={count:d}),c.groupCache[b].count>0&&(c.validFun.required(a)?c.groupCache[b].count-=1:"undefined"==typeof c.groupCache[b].element&&(c.groupCache[b].element=a)),!0},condRequired:function(a,b){var f,g,d=!1,e=!0;if(b.length){if(b=b.split(","),Array.isArray(b)&&b.length)for(d=!0,f=0,g=b.length;g>f;f++)if(!c.validFun.required(document.getElementById(b[f]))){d=!1;break}d&&(e=c.validFun.required(a))}return e},equals:function(a,b){return a.value==document.getElementById(b).value},minSize:function(a,b){return a.value.length&&a.value.length>=b},maxSize:function(a,b){return a.value.length&&a.value.length<=b},min:function(a,b){return a.value.length&&parseFloat(a.value)>=b},max:function(a,b){return a.value.length&&parseFloat(a.value)<=b},integer:function(a){return a.value.length&&/^[\-\+]?\d+$/.test(a.value)},number:function(a){return a.value.length&&/^[\-\+]?((([0-9]{1,3})([,][0-9]{3})*)|([0-9]+))?([\.]([0-9]+))?$/.test(a.value)},onlyNumber:function(a){return a.value.length&&/^[0-9]+$/.test(a.value)},onlyNumberSp:function(a){return a.value.length&&/^[0-9\ ]+$/.test(a.value)},onlyLetter:function(a){return a.value.length&&/^[a-zA-Z]+$/.test(a.value)},onlyLetterSp:function(a){return a.value.length&&/^[a-zA-Z\ ]+$/.test(a.value)},onlyLetterNumber:function(a){return a.value.length&&/^[0-9a-zA-Z]+$/.test(a.value)},onlyLetterNumberSp:function(a){return a.value.length&&/^[0-9a-zA-Z\ ]+$/.test(a.value)},email:function(a){return a.value.length&&/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(a.value)},phone:function(a){return a.value.length&&/^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/.test(a.value)},url:function(a){return a.value.length&&/^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(a.value)},chinese:function(a){return a.value.length&&/^[\u4E00-\u9FA5]+$/.test(a.value)},chinaId:function(a){return a.value.length&&/^[1-9]\d{5}[1-9]\d{3}(((0[13578]|1[02])(0[1-9]|[12]\d|3[0-1]))|((0[469]|11)(0[1-9]|[12]\d|30))|(02(0[1-9]|[12]\d)))(\d{4}|\d{3}[xX])$/.test(a.value)},chinaIdLoose:function(a){return a.value.length&&/^(\d{18}|\d{15}|\d{17}[xX])$/.test(a.value)},chinaZip:function(a){return a.value.length&&/^\d{6}$/.test(a.value)},qq:function(a){return a.value.length&&/^[1-9]\d{4,10}$/.test(a.value)},call:function(){var d,e,f,b=Array.prototype.slice.call(arguments),c=b.splice(1,1);if(c.indexOf(".")>=0){for(d=c.split("."),e=a;d.length;)e=e[d.shift()];f=e}else f=a[c];return"function"==typeof f?f.apply(e,b):!0}},c.init=function(){var a=this;a.groupCache={},a.vid=1,a.formFuns={},a.dom={},a.dom.tip=document.createElement("div"),a.dom.tip.classList.add("cxvalidation_tip"),document.addEventListener("DOMContentLoaded",function(){document.body.appendChild(a.dom.tip)})},c.getRuleArguments=function(a,b){var e,c=a.dataset.validation,d=[],f=[];return"string"==typeof c&&c.length&&(d=c.replace(/\s|\[[^\]]*\]/g,"").split(",")),"string"==typeof b&&b.length&&d.indexOf(b)>=0&&(e=c.match(new RegExp(b+"((\\[[^\\]]+\\])+)")),Array.isArray(e)&&e.length>1&&(f=e[1].match(/([^\[\]]+)/g),Array.isArray(f)&&1===f.length&&"groupRequired"===b&&(f[1]=1))),f},c.getMessage=function(a,b){var e,g,h,c=this,d="";if("string"==typeof a.dataset.validationMessage&&a.dataset.validationMessage.length)try{d=JSON.parse(a.dataset.validationMessage),"string"==typeof d[b]&&d[b].length&&(d=d[b])}catch(f){d=a.dataset.validationMessage}else"string"==typeof c.validMessage[b]&&c.validMessage[b].length&&(d=c.validMessage[b]);for(e=c.getRuleArguments(a,b),g=0,h=e.length;h>g;g++)d=d.replace(new RegExp("\\{\\{"+g+"\\}\\}"),e[g]);return d},c.validItem=function(a,b,c){var h,i,j,k,l,m,d=this,e=$.extend({},d.result),f=a.dataset.validation,g=[];if("function"==typeof b&&(b={success:b}),b=$.extend({},d.defaults,b),"function"==typeof c&&(b.error=c),"string"==typeof f&&f.length)for(g=f.replace(/\s|\[[^\]]*\]/g,"").split(","),k=0,l=g.length;l>k;k++)if(j=g[k],"function"==typeof d.validFun[j]&&(h=f.match(new RegExp(j+"((\\[[^\\]]+\\])+)")),i=[a],Array.isArray(h)&&h.length>1&&(i=i.concat(h[1].match(/([^\[\]]+)/g))),m=d.validFun[j].apply(d,i),"boolean"==typeof m?e.status=m:$.isPlainObject(e)&&"boolean"==typeof m.status&&(e=m),e.status===!1)){e.rule=j,"string"==typeof e.message&&e.message.length||(e.message=d.getMessage(a,j));break}return e.element=a,"function"==typeof b.complete&&b.complete(e),e.status===!0&&"function"==typeof b.success?b.success(e):e.status===!1&&"function"==typeof b.error&&b.error(e),"object"===b.type?e:e.status},c.validForm=function(a,b,c){var f,g,d=this,e=$.extend({},d.result);if("function"==typeof b&&(b={success:b}),b=$.extend({},d.defaults,b),"function"==typeof c&&(b.error=c),d.groupCache={},f=[].concat(Array.prototype.slice.call(a.querySelectorAll("input")),Array.prototype.slice.call(a.querySelectorAll("textarea")),Array.prototype.slice.call(a.querySelectorAll("select"))),$.each(f,function(a,b){var c=d.validItem(b,{type:"object"});return $.isPlainObject(c)&&c.status===!1?(e.element=b,e.status=!1,"string"==typeof c.rule&&c.rule.length&&(e.rule=c.rule),"string"==typeof c.message&&c.message.length&&(e.message=c.message),!1):void 0}),e.status===!0)for(g in d.groupCache)if(d.groupCache[g].count>0){e.status=!1,e.rule="groupRequired",e.element=d.groupCache[g].element,e.message=d.getMessage(e.element,e.rule);break}return d.groupCache={},e.form=a,"function"==typeof b.complete&&b.complete(e),e.status===!0&&"function"==typeof b.success?b.success(e):e.status===!1&&"function"==typeof b.error&&b.error(e),"object"===b.type?e:e.status},c.formSubmitFn=function(a,b){var c,d;event.preventDefault(),c=this,d={type:"object",success:function(){a.submit()},error:function(a){var b=a.element.nodeName.toLowerCase();"string"==typeof a.message&&a.message.length?c.isAndroid&&"select"===b?c.toMessage(a.element,a.message):c.toMessage(a.element,a.message,function(){c.toFocus(a.element)}):(c.dom.tip.classList.remove("show"),c.isAndroid&&"select"===b?c.toMessage(a.element):c.toFocus(a.element))}},b=$.extend({},d,b),c.validForm(a,b)},c.toMessage=function(a,b,c){var d=this;a.nodeName.toLowerCase(),"string"==typeof b&&b.length||(b="表单验证未通过"),"function"!=typeof c&&(c=function(){}),"function"==typeof $.cxDialog?$.cxDialog({title:"提示",info:b,ok:c}):("undefined"!=typeof d.closeTipWait&&clearTimeout(d.closeTipWait),d.dom.tip.innerHTML=b,d.dom.tip.classList.add("show"),c(),d.closeTipWait=setTimeout(function(){d.dom.tip.classList.remove("show")},3e3))},c.toFocus=function(a){var b=this,c=a.nodeName.toLowerCase();b.isVisible(a)&&(b.isAndroid?"select"===c?b.toMessage(a):a.focus():a.focus())},c.init(),d=function(){return d.valid.apply(d,arguments)},d.valid=function(){var a,d,e,f,b={};for(d=0,e=arguments.length;e>d;d++)c.isElement(arguments[d])?a=arguments[d]:c.isJquery(arguments[d])||c.isZepto(arguments[d])?a=arguments[d][0]:"function"==typeof arguments[d]?"function"==typeof b.success?b.error=arguments[d]:b.success=arguments[d]:"object"==typeof arguments[d]&&(b=$.extend(b,arguments[d]));return c.isElement(a)?(f=a.nodeName.toLowerCase(),"input"===f||"select"===f||"textarea"===f?c.validItem(a,b):c.validForm(a,b)):void 0},d.attach=function(a){var e,g,h,i,f={};for(g=0,h=arguments.length;h>g;g++)c.isJquery(arguments[g])||c.isZepto(arguments[g])||c.isElement(arguments[g])?e=c.isJquery(arguments[g])||c.isZepto(arguments[g])?arguments[g][0]:arguments[g]:"function"==typeof arguments[g]?"function"==typeof f.success?f.error=arguments[g]:f.success=arguments[g]:"object"==typeof arguments[g]&&(f=$.extend(f,arguments[g]));return c.isElement(e)&&e.nodeName&&"form"===e.nodeName.toLowerCase()?(i=e.dataset.cxVid,"function"!=typeof c.formFuns[i]?(i="cxValid_"+c.vid,e.dataset.cxVid=i,c.vid++):a.removeEventListener("submit",c.formFuns[i]),c.formFuns[i]=c.formSubmitFn.bind(c,e,f),e.addEventListener("submit",c.formFuns[i]),!0):!1},d.detach=function(a){if((c.isJquery(a)||c.isZepto(a))&&(a=a[0]),!c.isElement(a)||!a.nodeName||"form"!==a.nodeName.toLowerCase())return!1;var d=a.dataset.cxVid;return"function"!=typeof c.formFuns[d]?!1:(a.removeEventListener("submit",c.formFuns[d]),!0)},$.cxValidation=a.cxValidation=d}(window);