-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguardForm.min.js
1 lines (1 loc) · 3.52 KB
/
guardForm.min.js
1
HTMLElement.prototype.guardForm=function(){return this.querySelectorAll(".guard-input").forEach(e=>{let t=e.id,r=e.classList.contains("g-required"),s=e.classList.contains("g-number"),a=e.classList.contains("g-date"),l=e.classList.contains("g-email"),i=e.dataset.regex,n=i?RegExp(i):null,d=e.dataset.customRegex,o=d?RegExp(d):null,c=e.closest(".input-container");if(!c){(c=document.createElement("div")).classList.add("input-container"),c.style.marginBottom="1rem",e.parentNode.insertBefore(c,e),c.appendChild(e);let u=t.replace(/([A-Z])/g," $1");if(u=u.charAt(0).toUpperCase()+u.slice(1),!c.querySelector("label")){let p=document.createElement("label");p.setAttribute("for",t),p.textContent=u,c.insertBefore(p,e),c.insertBefore(document.createElement("br"),e)}if(r){let g=document.createElement("div");g.classList.add("error-message"),g.innerHTML="<p>Error: This field is required</p>",c.appendChild(g)}if(s){let m=document.createElement("div");m.classList.add("error-message","number-error"),m.innerHTML="<p>Invalid number: Please enter a valid number</p>",c.appendChild(m)}if(a){let v=document.createElement("div");v.classList.add("error-message","date-error"),v.innerHTML="<p>Invalid date: Please enter a valid date in the format MM/DD/YYYY</p>",c.appendChild(v)}if(l){let L=document.createElement("div");L.classList.add("error-message","email-error"),L.innerHTML="<p>Invalid email: Please enter a valid email address</p>",c.appendChild(L)}if(o){let y=document.createElement("div");y.classList.add("error-message","custom-error"),y.innerHTML="<p>Invalid format: Please use the correct format</p>",c.appendChild(y)}if(n){let f=document.createElement("div");f.classList.add("error-message","regex-error"),f.innerHTML="<p>Invalid format: Please use correct format</p>",c.appendChild(f)}}s&&e.addEventListener("input",function(){this.value=this.value.replace(/[^0-9]/g,"")}),a&&e.addEventListener("input",function(){let e=this.value.replace(/[^0-9]/g,""),t="";e.length>0&&(t+=e.substring(0,2)),e.length>2&&(t+="/"+e.substring(2,4)),e.length>4&&(t+="/"+e.substring(4,8)),this.value=t})}),this.addEventListener("submit",function(e){let t=!0;this.querySelectorAll(".guard-input").forEach(e=>{var r;let s=e.closest(".input-container"),a=e.value.trim(),l=e.classList.contains("g-required"),i=e.classList.contains("g-number"),n=e.classList.contains("g-date"),d=e.classList.contains("g-email"),o=e.dataset.regex,c=o?RegExp(o):null,u=e.dataset.customRegex,p=u?RegExp(u):null;s.classList.remove("g-invalid"),s.querySelectorAll(".error-message").forEach(e=>e.style.display="none"),l&&""===a?(s.classList.add("g-invalid"),s.querySelector(".error-message").style.display="block",t=!1):i&&isNaN(a)?(s.classList.add("g-invalid"),s.querySelector(".number-error").style.display="block",t=!1):n&&""!==a&&!function e(t){if(!/^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$/.test(t))return!1;let[r,s,a]=t.split("/").map(e=>parseInt(e,10)),l=new Date(a,r-1,s);return l.getMonth()===r-1&&l.getDate()===s&&l.getFullYear()===a}(a)?(s.classList.add("g-invalid"),s.querySelector(".date-error").style.display="block",t=!1):d?l&&""===a?(s.classList.add("g-invalid"),s.querySelector(".error-message").style.display="block",t=!1):""===a||(r=a,/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(r))||(s.classList.add("g-invalid"),s.querySelector(".email-error").style.display="block",t=!1):p&&""!==a&&!p.test(a)?(s.classList.add("g-invalid"),s.querySelector(".custom-error").style.display="block",t=!1):c&&""!==a&&!c.test(a)&&(s.classList.add("g-invalid"),s.querySelector(".regex-error").style.display="block",t=!1)}),t||e.preventDefault()}),this};