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