popup-validation
Advanced tools
Comparing version
@@ -54,2 +54,3 @@ (function (global, factory) { | ||
}`; | ||
const document = window.document; | ||
@@ -102,5 +103,5 @@ let _customSelector = ""; | ||
const forceToggleClass = (el, className, force) => { | ||
if (force && !el.classList.contains(className)){ | ||
if (force && !el.classList.contains(className)) { | ||
el.classList.add(className); | ||
} else if (!force && el.classList.contains(className)){ | ||
} else if (!force && el.classList.contains(className)) { | ||
el.classList.remove(className); | ||
@@ -135,3 +136,7 @@ } | ||
forceToggleClass(popup, ACTIVE, true); | ||
forceToggleClass(popup, "short-version", input.clientWidth < popup.clientWidth); | ||
forceToggleClass( | ||
popup, | ||
"short-version", | ||
input.clientWidth < popup.clientWidth | ||
); | ||
}, 0); | ||
@@ -173,3 +178,3 @@ }; | ||
const getEl = (el = "body") => | ||
typeof el === "string" ? document.querySelector(el) : el; | ||
typeof el === "string" ? document.querySelector(el) || document.body : el; | ||
@@ -293,6 +298,3 @@ const getInputs = el => el.querySelectorAll("." + VALIDATE); | ||
}); | ||
if ( | ||
_customSelector && | ||
el.querySelectorAll(_customSelector).length > 0 | ||
) { | ||
if (_customSelector && el.querySelectorAll(_customSelector).length > 0) { | ||
valid = false; | ||
@@ -327,9 +329,13 @@ } | ||
addClassValidation: selector => { | ||
const styles = selector.split(',').map(s => { | ||
return CUSTOM_CLASS_STYLES.replace(/\{0\}/gi, s); | ||
}).join(''); | ||
const styles = selector | ||
.split(",") | ||
.map(s => { | ||
return CUSTOM_CLASS_STYLES.replace(/\{0\}/gi, s); | ||
}) | ||
.join(""); | ||
const styleTag = document.createElement("style"); | ||
styleTag.innerHTML = styles; | ||
document.head.appendChild(styleTag); | ||
_customSelector = _customSelector + (_customSelector === "" ? "" : ",") + selector; | ||
_customSelector = | ||
_customSelector + (_customSelector === "" ? "" : ",") + selector; | ||
return validation; | ||
@@ -336,0 +342,0 @@ } |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.validation=t()}(this,function(){"use strict";var e={required:{message:"Required",method:function(e){if("checkbox"===e.type)return e.checked;if("radio"===e.type){var t=e.name;return e.parentNode.querySelectorAll("input[name="+t+"]:checked").length>0}return""!==e.value}},email:{message:"E-mail is wrong",method:function(e){return""===e.value||/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(e.value)}}},t=arguments,n={events:["change","paste","blur","keyup"]},i="",r=function(t){return t.getAttribute("data-validate").split(",").map(function(e){return e.trim()}).filter(function(n){return!e[n].method(t)}).map(function(t){return e[t].message}).join(", ")},a=function(e){var t=document.createElement("div");t.classList.add("validate-popup");var n=document.createElement("div");return n.appendChild(t.cloneNode(!0)),e.insertAdjacentHTML("beforebegin",n.innerHTML),t=e.previousElementSibling},o=function(e,t,n){n&&!e.classList.contains(t)?e.classList.add(t):!n&&e.classList.contains(t)&&e.classList.remove(t)},c=function(e,t){if(0!==e.offsetWidth||0!==e.offsetHeight){o(e,"validate-error",!0);var n=e.previousElementSibling;n&&n.matches(".validate-popup")||(n=a(e)),n.innerHTML=t,window.setTimeout(function(){var t=e.clientWidth<n.clientWidth?1+~~(e.offsetLeft+e.offsetWidth/2-14):e.offsetLeft+e.clientWidth-n.clientWidth+1,i=e.offsetTop;n.style.left=t+"px",n.style.top=i+"px",n.style.marginTop=-(n.clientHeight+8)+"px",o(n,"active",!0),o(n,"short-version",e.clientWidth<n.clientWidth)},0)}},u=function(e){o(e,"validate-error",!1);var t=e.previousElementSibling;t&&t.matches(".validate-popup")&&o(t,"active",!1)},l=function(e,t){t?c(e,t):u(e)},d=function(e){var t=e.target;if(t.matches(".validate")){var n=r(t);if("radio"===t.type){var i=t.name,a=document.querySelectorAll('input[name="'+i+'"]');[].forEach.call(a,function(e){return l(e,n)})}else l(t,n)}},f=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"body";return"string"==typeof e?document.querySelector(e):e},s=function(e){return e.querySelectorAll(".validate")},p=function(e){var t=e.target;if(t.matches(".validate-popup")){var n=t.nextElementSibling;n.matches(".validate")&&u(n)}},v=function(e){return!!y.validate(e.target)||(e.preventDefault(),!1)},m=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:250,i=arguments[2],r=void 0,a=void 0;return function(){var o=+new Date,c=t;r&&o<r+n?(clearTimeout(a),a=setTimeout(function(){r=o,e.apply(i,c)},n)):(r=o,e.apply(i,c))}}(function(){[].forEach.call(document.querySelectorAll(".validate-popup"),function(e){o(e,"active",!1)})},1e3),h=function(e,t){e=f(e);var i=t?"addEventListener":"removeEventListener",r=s(e);Array.prototype.forEach.call(r,function(e){n.events.forEach(function(t){e[i](t,d)})}),document[i]("click",p),"FORM"===e.nodeName&&e[i]("submit",v),window[i]("resize",m)},y={init:function(e,t){return Object.assign(n,t),h(e,!0),y},rules:e,destroy:function(e){return y.hide(e),h(e,!1),y},hide:function(e){e=f(e);var t=s(e);return Array.prototype.forEach.call(t,function(e){return u(e)}),y},highlight:function(e){e=f(e);var t=s(e);return Array.prototype.forEach.call(t,function(e){return d({target:e})}),y},isValid:function(e){e=f(e);var t=s(e),n=Array.prototype.every.call(t,function(e){return!r(e)});return i&&e.querySelectorAll(i).length>0&&(n=!1),n},validate:function(e){return y.highlight(e),y.isValid(e)},addClassValidation:function(e){var t=e.split(",").map(function(e){return"\n {0} {\n border-color: #D10000 !important;\n }\n {0}:before {\n opacity: 1;\n }\n {0}:after {\n opacity: 1;\n }".replace(/\{0\}/gi,e)}).join(""),n=document.createElement("style");return n.innerHTML=t,document.head.appendChild(n),i=i+(""===i?"":",")+e,y}};return y}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.validation=t()}(this,function(){"use strict";var e={required:{message:"Required",method:function(e){if("checkbox"===e.type)return e.checked;if("radio"===e.type){var t=e.name;return e.parentNode.querySelectorAll("input[name="+t+"]:checked").length>0}return""!==e.value}},email:{message:"E-mail is wrong",method:function(e){return""===e.value||/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(e.value)}}},t=arguments,n={events:["change","paste","blur","keyup"]},i=window.document,r="",a=function(t){return t.getAttribute("data-validate").split(",").map(function(e){return e.trim()}).filter(function(n){return!e[n].method(t)}).map(function(t){return e[t].message}).join(", ")},o=function(e){var t=i.createElement("div");t.classList.add("validate-popup");var n=i.createElement("div");return n.appendChild(t.cloneNode(!0)),e.insertAdjacentHTML("beforebegin",n.innerHTML),t=e.previousElementSibling},c=function(e,t,n){n&&!e.classList.contains(t)?e.classList.add(t):!n&&e.classList.contains(t)&&e.classList.remove(t)},l=function(e,t){if(0!==e.offsetWidth||0!==e.offsetHeight){c(e,"validate-error",!0);var n=e.previousElementSibling;n&&n.matches(".validate-popup")||(n=o(e)),n.innerHTML=t,window.setTimeout(function(){var t=e.clientWidth<n.clientWidth?1+~~(e.offsetLeft+e.offsetWidth/2-14):e.offsetLeft+e.clientWidth-n.clientWidth+1,i=e.offsetTop;n.style.left=t+"px",n.style.top=i+"px",n.style.marginTop=-(n.clientHeight+8)+"px",c(n,"active",!0),c(n,"short-version",e.clientWidth<n.clientWidth)},0)}},u=function(e){c(e,"validate-error",!1);var t=e.previousElementSibling;t&&t.matches(".validate-popup")&&c(t,"active",!1)},f=function(e,t){t?l(e,t):u(e)},d=function(e){var t=e.target;if(t.matches(".validate")){var n=a(t);if("radio"===t.type){var r=t.name,o=i.querySelectorAll('input[name="'+r+'"]');[].forEach.call(o,function(e){return f(e,n)})}else f(t,n)}},s=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"body";return"string"==typeof e?i.querySelector(e)||i.body:e},p=function(e){return e.querySelectorAll(".validate")},v=function(e){var t=e.target;if(t.matches(".validate-popup")){var n=t.nextElementSibling;n.matches(".validate")&&u(n)}},h=function(e){return!!g.validate(e.target)||(e.preventDefault(),!1)},m=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:250,i=arguments[2],r=void 0,a=void 0;return function(){var o=+new Date,c=t;r&&o<r+n?(clearTimeout(a),a=setTimeout(function(){r=o,e.apply(i,c)},n)):(r=o,e.apply(i,c))}}(function(){[].forEach.call(i.querySelectorAll(".validate-popup"),function(e){c(e,"active",!1)})},1e3),y=function(e,t){e=s(e);var r=t?"addEventListener":"removeEventListener",a=p(e);Array.prototype.forEach.call(a,function(e){n.events.forEach(function(t){e[r](t,d)})}),i[r]("click",v),"FORM"===e.nodeName&&e[r]("submit",h),window[r]("resize",m)},g={init:function(e,t){return Object.assign(n,t),y(e,!0),g},rules:e,destroy:function(e){return g.hide(e),y(e,!1),g},hide:function(e){e=s(e);var t=p(e);return Array.prototype.forEach.call(t,function(e){return u(e)}),g},highlight:function(e){e=s(e);var t=p(e);return Array.prototype.forEach.call(t,function(e){return d({target:e})}),g},isValid:function(e){e=s(e);var t=p(e),n=Array.prototype.every.call(t,function(e){return!a(e)});return r&&e.querySelectorAll(r).length>0&&(n=!1),n},validate:function(e){return g.highlight(e),g.isValid(e)},addClassValidation:function(e){var t=e.split(",").map(function(e){return"\n {0} {\n border-color: #D10000 !important;\n }\n {0}:before {\n opacity: 1;\n }\n {0}:after {\n opacity: 1;\n }".replace(/\{0\}/gi,e)}).join(""),n=i.createElement("style");return n.innerHTML=t,i.head.appendChild(n),r=r+(""===r?"":",")+e,g}};return g}); |
{ | ||
"name": "popup-validation", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"description": "Pure JavaScript/CSS library for validating DOM input fields", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
42320
0.16%745
0.81%