forked from ryanseddon/H5F
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh5f.min.js
4 lines (4 loc) · 5.47 KB
/
h5f.min.js
1
2
3
4
/*! H5F
* https://github.com/ryanseddon/H5F/
* Copyright (c) Ryan Seddon | Licensed MIT */
(function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof module&&module.exports?module.exports=t():e.H5F=t()})(this,function(){var e,t,i,a,n,r,s,l,o,u,d,c,v,p,f,m,g,h,b,y,w,C,N,A,E,$=document,x=$.createElement("input"),k=/^[a-zA-Z0-9.!#$%&'*+-\/=?\^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/,q=/[a-z][\-\.+a-z]*:\/\//i,I=/^(input|select|textarea)$/i;return n=function(e,t){var i=!e.nodeType||!1,a={validClass:"valid",invalidClass:"error",requiredClass:"required",placeholderClass:"placeholder",onSubmit:Function.prototype,onInvalid:Function.prototype};if("object"==typeof t)for(var n in a)t[n]===void 0&&(t[n]=a[n]);if(e.h5fSettings=t||a,i)for(var s=0,l=e.length;l>s;s++)r(e[s]);else r(e)},r=function(i){var a,n=i.elements,r=n.length,d=!!i.attributes.novalidate;if(h(i,"invalid",l,!0),h(i,"blur",l,!0),h(i,"input",l,!0),h(i,"keyup",l,!0),h(i,"focus",l,!0),h(i,"change",l,!0),h(i,"click",o,!0),h(i,"submit",function(a){return e=!0,t||d||i.checkValidity()?(i.h5fSettings.onSubmit.call(i,a),void 0):(y(a),void 0)},!1),!c())for(i.checkValidity=function(){return u(i)};r--;)a=!!n[r].attributes.required,"fieldset"!==n[r].nodeName.toLowerCase()&&s(n[r])},s=function(e){var t=e,i=g(t),n={type:t.getAttribute("type"),pattern:t.getAttribute("pattern"),placeholder:t.getAttribute("placeholder")},r=/^(email|url)$/i,s=/^(input|keyup)$/i,l=r.test(n.type)?n.type:n.pattern?n.pattern:!1,o=v(t,l),c=f(t,"step"),m=f(t,"min"),h=f(t,"max"),b=!(""===t.validationMessage||void 0===t.validationMessage);t.checkValidity=function(){return u.call(this,t)},t.setCustomValidity=function(e){d.call(t,e)},t.validity={valueMissing:i,patternMismatch:o,rangeUnderflow:m,rangeOverflow:h,stepMismatch:c,customError:b,valid:!(i||o||c||m||h||b)},n.placeholder&&!s.test(a)&&p(t)},l=function(e){var t,i,n=w(e)||e,r=/^(input|keyup|focusin|focus|change)$/i,o=/^(submit|image|button|reset)$/i,u=/^(checkbox|radio)$/i,d=!0;!I.test(n.nodeName)||o.test(n.type)||o.test(n.nodeName)||(t=n.form,i=t.h5fSettings,a=e.type,c()||s(n),n.validity.valid&&(""!==n.value||u.test(n.type))||n.value!==n.getAttribute("placeholder")&&n.validity.valid?(N(n,[i.invalidClass,i.requiredClass]),C(n,i.validClass)):r.test(a)?n.validity.valueMissing&&N(n,[i.requiredClass,i.invalidClass,i.validClass]):n.validity.valueMissing?(N(n,[i.invalidClass,i.validClass]),C(n,i.requiredClass)):n.validity.valid||(N(n,[i.validClass,i.requiredClass]),C(n,i.invalidClass)),"input"===a&&d&&(b(t,"keyup",l,!0),d=!1))},u=function(t){var i,a,n,r,s,o=!1,u=t.h5fSettings;if("form"===t.nodeName.toLowerCase()){i=t.elements;for(var d=0,c=i.length;c>d;d++)a=i[d],n=!!a.attributes.disabled,r=!!a.attributes.required,s=!!a.attributes.pattern,"fieldset"!==a.nodeName.toLowerCase()&&!n&&(r||s&&r)&&(l(a),a.validity.valid||(e&&!o&&a.focus(),o=!0,u.onInvalid.call(t,a)));return!o}return l(t),t.validity.valid},d=function(e){var t=this;t.validationMessage=e},o=function(e){var i=w(e);i.attributes.formnovalidate&&"submit"===i.type&&(t=!0)},c=function(){return A(x,"validity")&&A(x,"checkValidity")},v=function(e,t){if("email"===t)return!k.test(e.value);if("url"===t)return!q.test(e.value);if(t){var a=e.getAttribute("placeholder"),n=e.value;return i=RegExp("^(?:"+t+")$"),n===a?!1:""===n?!1:!i.test(e.value)}return!1},p=function(e){var t={placeholder:e.getAttribute("placeholder")},i=/^(focus|focusin|submit)$/i,n=/^(input|textarea)$/i,r=/^password$/i,s=!!("placeholder"in x),l=e.form.h5fSettings;s||!n.test(e.nodeName)||r.test(e.type)||(""!==e.value||i.test(a)?e.value===t.placeholder&&i.test(a)&&(e.value="",N(e,l.placeholderClass)):(e.value=t.placeholder,h(e.form,"submit",function(){a="submit",p(e)},!0),C(e,l.placeholderClass)))},f=function(e,t){var i=parseInt(e.getAttribute("min"),10)||0,a=parseInt(e.getAttribute("max"),10)||!1,n=parseInt(e.getAttribute("step"),10)||1,r=parseInt(e.value,10),s=(r-i)%n;return g(e)||isNaN(r)?"number"===e.getAttribute("type")?!0:!1:"step"===t?e.getAttribute("step")?0!==s:!1:"min"===t?e.getAttribute("min")?i>r:!1:"max"===t?e.getAttribute("max")?r>a:!1:void 0},m=function(e){var t=!!e.attributes.required;return t?g(e):!1},g=function(e){var t=e.getAttribute("placeholder"),i=/^(checkbox|radio)$/i,a=!!e.attributes.required;return!(!a||""!==e.value&&e.value!==t&&(!i.test(e.type)||E(e)))},h=function(e,t,i,a){A(window,"addEventListener")?e.addEventListener(t,i,a):A(window,"attachEvent")&&window.event!==void 0&&("blur"===t?t="focusout":"focus"===t&&(t="focusin"),e.attachEvent("on"+t,i))},b=function(e,t,i,a){A(window,"removeEventListener")?e.removeEventListener(t,i,a):A(window,"detachEvent")&&window.event!==void 0&&e.detachEvent("on"+t,i)},y=function(e){e=e||window.event,e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopImmediatePropagation?e.stopImmediatePropagation():e.stopPropagation?e.stopPropagation():e.cancelBubble=!0},w=function(e){return e=e||window.event,e.target||e.srcElement},C=function(e,t){var i;e.className?(i=RegExp("(^|\\s)"+t+"(\\s|$)"),i.test(e.className)||(e.className+=" "+t)):e.className=t},N=function(e,t){var i,a,n="object"==typeof t?t.length:1,r=n;if(e.className)if(e.className===t)e.className="";else for(;n--;)i=RegExp("(^|\\s)"+(r>1?t[n]:t)+"(\\s|$)"),a=e.className.match(i),a&&3===a.length&&(e.className=e.className.replace(i,a[1]&&a[2]?" ":""))},A=function(e,t){var i=typeof e[t],a=RegExp("^function|object$","i");return!!(a.test(i)&&e[t]||"unknown"===i)},E=function(e){for(var t=document.getElementsByName(e.name),i=0;t.length>i;i++)if(t[i].checked)return!0;return!1},{setup:n,support:c}});