-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigleFormValidate.min.js
1 lines (1 loc) · 3.18 KB
/
sigleFormValidate.min.js
1
!function(e,a){var t=function(){this.custom={},this.type={},this.defaultType={int:{defaultMsg:"该项只能为数字",reg:/^\d+$/},phone:{defaultMsg:"手机号不正确",reg:/^1[3-9][0-9]{9}$/}},this.scope=a,this.counter=0,this.errorArray=[]};t.prototype={buildRelatedMethods:function(){var t=this;Object.keys(t.type).forEach(function(e){t[e+"Validate"]=new Function("e","type",'var $target = this; var msg = e.getAttribute("s-"+type+"-msg") || $target.type[type].defaultMsg;\n if ($target.type[type].reg.test(e.value)) {\n $target.removeErrorMsg(e, type)\n } else {\n $target.appendErrorMsg(e, msg, type);\n }')})},init:function(e,t){var r=this;e=e||"body";r.type=Object.assign(r.defaultType,r.custom),r.scope=e,r.buildRelatedMethods(),null!==t&&"function"==typeof t&&(r.appendErrorMsg=t);var n=a.querySelector(e);n.addEventListener("change",function(e){r.sortValidate(e.target)}),n.addEventListener("keyup",function(e){r.sortValidate(e.target)}),n.addEventListener("blur",function(e){r.sortValidate(e.target)})},sortValidate:function(e){var t=this,r=e.attributes;if(""===e.value)t.removeAllErrorMsg(e),null!==e.getAttribute("s-req")&&t.reqValidate(e);else for(var n=0,a=r.length;n<a;n++)if(/^s-\w+$/.test(r[n].name)){var s=r[n].name.split("").splice(2).join("");t[s+"Validate"].call(t,e,s)}},reqValidate:function(e){var t=e.getAttribute("s-req-msg")||"该项不能为空";""===e.value&&null!==e.getAttribute("s-req")?this.appendErrorMsg(e,t,"req"):this.removeErrorMsg(e,"req")},maxValidate:function(e){var t=e.getAttribute("s-max"),r=e.getAttribute("s-max-msg")||"该项最大值不超过"+t+"个字符";e.value.length>t?this.appendErrorMsg(e,r,"max"):this.removeErrorMsg(e,"max")},minValidate:function(e){var t=e.getAttribute("s-min"),r=e.getAttribute("s-min-msg")||"该项不低于"+t+"个字符";e.value.length<t?this.appendErrorMsg(e,r,"max"):this.removeErrorMsg(e,"max")},compareValidate:function(e){var t=e.getAttribute("s-compare"),r=e.getAttribute("s-compare-msg")||"两项比较不一致",n=a.querySelector(t);e.value!==n.value?this.appendErrorMsg(e,r,"max"):this.removeErrorMsg(e,"max")},appendErrorMsg:function(e,t,r){var n=a.createElement("span");n.style.color="red",n.style.display="block",n.style.fontSize="12px",n.className="s-err-msg",n.textContent=t,n.setAttribute("data-flag",r),this.removeAllErrorMsg(e,r),e.insertAdjacentElement("afterend",n)},removeAllErrorMsg:function(e,t){var r=this.findNextErrorElement(e);r&&r.length&&(r.forEach(function(e){e.remove()}),this.errorArray=[])},removeErrorMsg:function(e,t){this.findNextErrorElement(e).forEach(function(e){e.dataset.flag===t&&e.remove()}),this.errorArray=[]},findNextErrorElement:function(e){var t=this;return e.nextElementSibling&&e.nextElementSibling.classList.contains("s-err-msg")?(t.errorArray.push(e.nextElementSibling),t.findNextErrorElement(e.nextElementSibling)):t.errorArray},validate:function(){var t=this;return a.querySelector(t.scope).querySelectorAll("input[s-req],select[s-req],textarea[s-req]").forEach(function(e){t.sortValidate(e)}),t.counter=a.querySelector(t.scope).getElementsByClassName("s-err-msg").length,0===t.counter}},e.Sfv=t}(window,document);