@jdlien/validator 1.4.1 → 1.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/Validator.d.ts +3 -0
- package/dist/validator.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -101,11 +101,11 @@ If you wish to customize the default error message, you can also set one for a f
|
|
|
101
101
|
## Supported Input Types and Atributes
|
|
102
102
|
|
|
103
103
|
Validator works by checking for certain attributes on the form inputs and applying validation based on those.
|
|
104
|
-
In many cases you can use the native HTML5 attributes, but you can also use the `data-` attributes if you do not want the behavior to be affected by built-in browser validation behavior (
|
|
104
|
+
In many cases you can use the native HTML5 attributes, but you can also use the `data-` attributes if you do not want the behavior to be affected by built-in browser validation behavior (e.g., for min-length, max-length, and input types such as date and time).
|
|
105
105
|
|
|
106
106
|
There are a few attributes that Validator looks for on the form element:
|
|
107
107
|
|
|
108
|
-
- `data-prevent-submit` - If this attribute is present, the form will never be submitted, even is valid. This is useful if you want to handle the submission yourself. (By default the form will be submitted if it is valid and not if it is invalid.)
|
|
108
|
+
- `data-prevent-submit` - If this attribute is present, the form will never be submitted, even if it is valid. This is useful if you want to handle the submission yourself. (By default the form will be submitted if it is valid and not if it is invalid.)
|
|
109
109
|
|
|
110
110
|
- `novalidate` - This is a native HTML5 attribute that will disable browser validation on the form. If this attribute is present. Validator adds this by default and remove it if `destroy()` is called. If you add it yourself, it will not be added back by Validator.
|
|
111
111
|
|
|
@@ -226,7 +226,7 @@ messages = {
|
|
|
226
226
|
- `debug` - A boolean indicating whether or not to show debug messages in the console. Defaults to false.
|
|
227
227
|
- `autoInit` - A boolean indicating whether or not to automatically initialize the Validator instance on page load. Defaults to true.
|
|
228
228
|
- `preventSubmit` - A boolean indicating whether or not to prevent form submission if validation is successful. Defaults to false.
|
|
229
|
-
- `hiddenClasses` - A string containing one or more space-separated classes to toggle the hidden mode (
|
|
229
|
+
- `hiddenClasses` - A string containing one or more space-separated classes to toggle the hidden mode (e.g., `display: none` CSS property) on hidden elements. Defaults to `hidden opacity-0`.
|
|
230
230
|
- `errorMainClasses` - A string containing one or more space-separated classes to apply to the main error message.
|
|
231
231
|
- `errorInputClasses` - A string containing one or more space-separated classes to apply to invalid `inputs.
|
|
232
232
|
- `validationSuccessCallback` - A function to be called when validation is successful.
|
package/dist/Validator.d.ts
CHANGED
|
@@ -64,10 +64,13 @@ export default class Validator {
|
|
|
64
64
|
errorMainClasses: string;
|
|
65
65
|
errorInputClasses: string;
|
|
66
66
|
private dispatchTimeout;
|
|
67
|
+
private timeoutId;
|
|
68
|
+
private debouncedInit;
|
|
67
69
|
private originalNoValidate;
|
|
68
70
|
private validationSuccessCallback;
|
|
69
71
|
private validationErrorCallback;
|
|
70
72
|
constructor(form: HTMLFormElement, options?: ValidatorOptions);
|
|
73
|
+
debounce<T extends (...args: any[]) => any>(func: T, wait: number): (...funcArgs: Parameters<T>) => void;
|
|
71
74
|
private submitHandlerRef;
|
|
72
75
|
private inputInputHandlerRef;
|
|
73
76
|
private inputChangeHandlerRef;
|
package/dist/validator.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(R,E){typeof exports=="object"&&typeof module<"u"?module.exports=E():typeof define=="function"&&define.amd?define(E):(R=typeof globalThis<"u"?globalThis:R||self,R.Validator=E())})(this,function(){"use strict";var oe=Object.defineProperty;var le=(R,E,o)=>E in R?oe(R,E,{enumerable:!0,configurable:!0,writable:!0,value:o}):R[E]=o;var m=(R,E,o)=>(le(R,typeof E!="symbol"?E+"":E,o),o);var R=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},E={exports:{}};(function(L,t){(function(r,n){n(t)})(R,function(r){function n(e){return e instanceof HTMLInputElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement}function u(e,a){typeof a=="string"&&(a=[a]);const s=e.dataset.type||"",i=e.type;return!!(a.includes(s)||a.includes(i))}function g(e){return e.replace(/YYYY/g,"Y").replace(/YY/g,"y").replace(/MMMM/g,"F").replace(/MMM/g,"{3}").replace(/MM/g,"{2}").replace(/M/g,"n").replace(/DD/g,"{5}").replace(/D/g,"j").replace(/dddd/g,"l").replace(/ddd/g,"D").replace(/dd/g,"D").replace(/d/g,"w").replace(/HH/g,"{6}").replace(/H/g,"G").replace(/hh/g,"h").replace(/mm/g,"i").replace(/m/g,"i").replace(/ss/g,"S").replace(/s/g,"s").replace(/A/gi,"K").replace(/\{3\}/g,"M").replace(/\{2\}/g,"m").replace(/\{5\}/g,"d").replace(/\{6\}/g,"H")}function f(e){const a=parseInt(e);if(typeof e=="number"||!isNaN(a))return a-1;const s=new Date(`1 ${e} 2000`).getMonth();if(!isNaN(s))return s;const i={ja:0,en:0,fe:1,fé:1,ap:3,ab:3,av:3,mai:4,juin:5,juil:6,au:7,ag:7,ao:7,se:8,o:9,n:10,d:11};for(const c in i)if(e.toLowerCase().startsWith(c))return i[c];throw new Error("Invalid month name: "+e)}function T(e){return typeof e=="string"&&(e=parseInt(e.replace(/\D/g,""))),e>99?e:e<(new Date().getFullYear()+20)%100?e+2e3:e+1900}function w(e){if(e instanceof Date)return e;e=e.trim().toLowerCase();let a=0,s=0,i=0,c=0,d=0,l=0;const p=new RegExp(/\d{1,2}\:\d\d(?:\:\d\ds?)?\s?(?:[a|p]m?)?/gi);if(p.test(e)){const v=e.match(p)[0];e=e.replace(v,"").trim();const y=I(v);if(y!==null&&({hour:c,minute:d,second:l}=y),e.length<=2){const N=new Date;return new Date(N.getFullYear(),N.getMonth(),N.getDate(),c,d,l)}}const b=/(^|\b)(mo|tu|we|th|fr|sa|su|lu|mard|mer|jeu|ve|dom)[\w]*\.?/gi;e=e.replace(b,"").trim();const h=new Date(new Date().setHours(0,0,0,0));if(/(now|today)/.test(e))return h;if(e.includes("tomorrow"))return new Date(h.setDate(h.getDate()+1));e.length===8&&(e=e.replace(/(\d\d\d\d)(\d\d)(\d\d)/,"$1-$2-$3")),e.length===6&&(e=e.replace(/(\d\d)(\d\d)(\d\d)/,T(e.slice(0,2))+"-$2-$3"));try{({year:a,month:s,day:i}=x(e))}catch{return new Date("")}return new Date(a,s-1,i,c,d,l)}function _(e,a=[null,null,null]){const s=i=>i.filter(c=>!a.includes(c));return e===0||e>31?s(["y"]):e>12?s(["d","y"]):e>=1&&e<=12?s(["m","d","y"]):[]}function x(e){const a=e.split(/[\s-/:.,]+/).filter(d=>d!=="");if(a.length<3){if(e.match(/\d{4}/)!==null)throw new Error("Invalid Date");a.unshift(String(new Date().getFullYear()))}const s={year:0,month:0,day:0};function i(d,l){d==="year"?s.year=T(l):s[d]=l}let c=0;for(;!(s.year&&s.month&&s.day);){e:for(const d of a){if(c++,/^[a-zA-Zé]+$/.test(d)){s.month||i("month",f(d)+1);continue}if(/^'\d\d$/.test(d)||/^\d{3,5}$/.test(d)){s.year||i("year",parseInt(d.replace(/'/,"")));continue}const l=parseInt(d);if(isNaN(l))throw console.error(`not date because ${d} isNaN`),new Error("Invalid Date");const p=_(l,[s.year?"y":null,s.month?"m":null,s.day?"d":null]);if(p.length==1){if(p[0]==="m"&&!s.month){i("month",l);continue e}if(p[0]==="d"&&!s.day){i("day",l);continue e}if(p[0]==="y"&&!s.year){i("year",l);continue e}}c>3&&(!s.month&&p.includes("m")?i("month",l):!s.day&&p.includes("d")&&i("day",l))}if(c>6)throw new Error("Invalid Date")}if(s.year&&s.month&&s.day)return s;throw new Error("Invalid Date")}function I(e){if(e=e.trim().toLowerCase(),e==="now"){const h=new Date;return{hour:h.getHours(),minute:h.getMinutes(),second:h.getSeconds()}}const a=e.match(/(\d{3,4})/);if(a){const h=a[1].length,v=a[1].slice(0,h==3?1:2),y=a[1].slice(-2);e=e.replace(a[1],v+":"+y)}const s=new RegExp(/^(\d{1,2})(?::(\d{1,2}))?\s*(?:(a|p)m?)?$/i);if(s.test(e)){const h=e.match(s);if(h===null)return null;e=h[1]+":"+(h[2]||"00")+(h[3]||"")}const i=new RegExp(/^(\d{1,2}):(\d{1,2})(?::(\d{1,2}))?\s*(?:(a|p)m?)?$/i);if(!i.test(e))return null;const c=e.match(i);if(c===null)return null;const d=parseInt(c[1]),l=parseInt(c[2]),p=c[3]?parseInt(c[3]):0,b=c[4];return isNaN(d)||isNaN(l)||isNaN(p)?null:b==="p"&&d<12?{hour:d+12,minute:l,second:p}:b==="a"&&d===12?{hour:0,minute:l,second:p}:d<0||d>23||l<0||l>59||p<0||p>59?null:{hour:d,minute:l,second:p}}function $(e,a="h:mm A"){const s=I(e);if(s){const i=new Date;return i.setHours(s.hour),i.setMinutes(s.minute),i.setSeconds(s.second),i.setMilliseconds(0),M(i,a)}return""}function S(e){if(e instanceof Date)return e;if(e.trim().length<3)return null;let a=e.split(/[\s,]+/).filter(l=>l!==""),s="",i="";a.forEach(l=>{/^\d{1,2}:\d{1,2}(:\d{2})?(.*)$/.test(l)?s=l:A(l)&&(i=l)}),a=a.filter(l=>l!==s&&l!==i);const c=I(s+" "+i)||{hour:0,minute:0,second:0},d=w(a.join(" ").trim()||"today");return!d||isNaN(d.getTime())?null:new Date(d.getFullYear(),d.getMonth(),d.getDate(),c.hour,c.minute,c.second)}function M(e,a="YYYY-MM-DD"){if(e=w(e),isNaN(e.getTime()))return"";const s={y:e.getFullYear(),M:e.getMonth(),D:e.getDate(),W:e.getDay(),H:e.getHours(),m:e.getMinutes(),s:e.getSeconds(),ms:e.getMilliseconds()},i=(h,v=2)=>(h+"").padStart(v,"0"),c=()=>s.H%12||12,d=h=>h<12?"AM":"PM",l=h=>"January|February|March|April|May|June|July|August|September|October|November|December".split("|")[h];function p(h,v=0){const y="Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday".split("|");return v?y[h].slice(0,v):y[h]}const b={YY:String(s.y).slice(-2),YYYY:s.y,M:s.M+1,MM:i(s.M+1),MMMM:l(s.M),MMM:l(s.M).slice(0,3),D:String(s.D),DD:i(s.D),d:String(s.W),dd:p(s.W,2),ddd:p(s.W,3),dddd:p(s.W),H:String(s.H),HH:i(s.H),h:c(),hh:i(c()),A:d(s.H),a:d(s.H).toLowerCase(),m:String(s.m),mm:i(s.m),s:String(s.s),ss:i(s.s),SSS:i(s.ms,3)};return a.replace(/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,(h,v)=>v||b[h])}function Y(e,a){const s=w(e);return isNaN(s.getTime())?"":((!a||a.length===0)&&(a="YYYY-MMM-DD"),M(s,a))}function V(e,a){const s=S(e);return s===null||isNaN(s.getTime())?"":((!a||a.length===0)&&(a="YYYY-MMM-DD h:mm A"),M(s,a))}function P(e){if(typeof e!="string"&&!(e instanceof Date))return!1;let a=w(e);return a==null?!1:!isNaN(a.getTime())}function U(e){if(typeof e!="string"&&!(e instanceof Date))return!1;let a=S(e);return a==null?!1:!isNaN(a.getTime())}function F(e,a){return!(a==="past"&&e>new Date||a==="future"&&e.getTime()<new Date().setHours(0,0,0,0))}function A(e){const a=e.toLowerCase().replace(/[.\s]/g,"");return["am","pm","a","p"].includes(a)}function q(e){let a=I(e);return a===null?!1:!isNaN(a.hour)&&!isNaN(a.minute)&&!isNaN(a.second)}function Z(e){if(e.length>255||!new RegExp(/^.+@.+\.[a-zA-Z0-9]{2,}$/).test(e))return!1;let a="";return a+="^([a-zA-Z0-9!#$%'*+/=?^_`{|}~-]+",a+="(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*",a+="|",a+='"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*"',a+=")@(",a+="(",a+="(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+",a+="[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?",a+=")",a+=")$",new RegExp(a).test(e)}function z(e){return e=e.replace(/^[^2-90]+/g,""),e=e.replace(/(\d\d\d).*?(\d\d\d).*?(\d\d\d\d)(.*)/,"$1-$2-$3$4"),e}function G(e){return/^\d\d\d-\d\d\d-\d\d\d\d$/.test(e)}function j(e){return e.replace(/[^0-9]/g,"")}function B(e){return/^\-?\d*\.?\d*$/.test(e)}function K(e){return e.replace(/[^\-0-9.]/g,"").replace(/(^-)|(-)/g,(a,s)=>s?"-":"").replace(/(\..*)\./g,"$1")}function W(e){return/^\-?\d*$/.test(e)}function J(e){return e=e.trim(),new RegExp("^(?:[a-z+]+:)?//","i").test(e)?e:"https://"+e}function Q(e){return new RegExp("^(?:[-a-z+]+:)?//","i").test(e)}function X(e){return e=e.replace(/[^0-9]/g,"").replace(/(.{5})(.*)/,"$1-$2").trim(),e.length===6&&(e=e.replace(/-/,"")),e}function ee(e){return new RegExp(/^\d{5}(-\d{4})?$/).test(e)}function te(e){return e=e.toUpperCase().replace(/[^A-Z0-9]/g,"").replace(/(.{3})\s*(.*)/,"$1 $2").trim(),e}function re(e){return new RegExp(/^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$/).test(e)}function ae(e){return["transparent","currentColor"].includes(e)?!0:typeof e!="string"||!e.trim()?!1:typeof CSS=="object"&&typeof CSS.supports=="function"?CSS.supports("color",e):se(e)}function se(e){const a=new RegExp(/^rgba?\(\s*(\d{1,3}%?,\s*){2}\d{1,3}%?\s*(?:,\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),s=new RegExp(/^hsla?\(\s*\d+(deg|grad|rad|turn)?,\s*\d{1,3}%,\s*\s*\d{1,3}%(?:,\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),i=new RegExp(/^rgba?\(\s*(\d{1,3}%?\s+){2}\d{1,3}%?\s*(?:\s*\/\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),c=new RegExp(/^hsla?\(\s*\d+(deg|grad|rad|turn)?\s+\d{1,3}%\s+\s*\d{1,3}%(?:\s*\/\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),d=new RegExp(/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i);let l="aliceblue|antiquewhite|aqua|aquamarine|azure|beige|bisque|black|blanchedalmond|blue|blueviolet|brown|burlywood|cadetblue|chartreuse|chocolate|coral|cornflowerblue|cornsilk|crimson|cyan|darkblue|darkcyan|darkgoldenrod|darkgray|darkgreen|darkgrey|darkkhaki|darkmagenta|darkolivegreen|darkorange|darkorchid|darkred|darksalmon|darkseagreen|darkslateblue|darkslategray|darkslategrey|darkturquoise|darkviolet|deeppink|deepskyblue|dimgray|dimgrey|dodgerblue|firebrick|floralwhite|forestgreen|fuchsia|gainsboro|ghostwhite|gold|goldenrod|gray|green|greenyellow|grey|honeydew|hotpink|indianred|indigo|ivory|khaki|lavender|lavenderblush|lawngreen|lemonchiffon|lightblue|lightcoral|lightcyan|lightgoldenrodyellow|lightgray|lightgreen|lightgrey|lightpink|lightsalmon|lightseagreen|lightskyblue|lightslategray|lightslategrey|lightsteelblue|lightyellow|lime|limegreen|linen|magenta|maroon|mediumaquamarine|mediumblue|mediumorchid|mediumpurple|mediumseagreen|mediumslateblue|mediumspringgreen|mediumturquoise|mediumvioletred|midnightblue|mintcream|mistyrose|moccasin|navajowhite|navy|oldlace|olive|olivedrab|orange|orangered|orchid|palegoldenrod|palegreen|paleturquoise|palevioletred|papayawhip|peachpuff|peru|pink|plum|powderblue|purple|rebeccapurple|red|rosybrown|royalblue|saddlebrown|salmon|sandybrown|seagreen|seashell|sienna|silver|skyblue|slateblue|slategray|slategrey|snow|springgreen|steelblue|tan|teal|thistle|tomato|turquoise|violet|wheat|white|whitesmoke|yellow|yellowgreen";const p=new RegExp(`^(${l})$`,"i");return a.test(e)||s.test(e)||i.test(e)||c.test(e)||d.test(e)||p.test(e)}let D=null;const C=new Map;function ne(e){if(e=e.trim().toLowerCase(),["transparent","currentcolor"].includes(e))return e;if(C.has(e))return C.get(e);D===null&&(D=document.createElement("canvas"),D.willReadFrequently=!0);let a=D.getContext("2d");if(!a)throw new Error("Can't get context from colorCanvas");a.fillStyle=e,a.fillRect(0,0,1,1);let s=a.getImageData(0,0,1,1).data,i="#"+("000000"+(s[0]<<16|s[1]<<8|s[2]).toString(16)).slice(-6);return C.set(e,i),i}function ie(e){let a={valid:!1,error:!1,messages:[]};return typeof e=="boolean"?{valid:e,error:!1,messages:[]}:typeof e=="string"?{valid:!1,error:!1,messages:[e]}:(typeof e.valid=="boolean"&&(a.valid=e.valid),typeof e.message=="string"&&(a.messages=[e.message]),typeof e.messages=="string"&&(a.messages=[e.messages]),Array.isArray(e.messages)&&(a.messages=e.messages),e.error===!0&&(a.error=!0),a)}r.formatDateTime=M,r.isColor=ae,r.isDate=P,r.isDateInRange=F,r.isDateTime=U,r.isEmail=Z,r.isFormControl=n,r.isInteger=W,r.isMeridiem=A,r.isNANPTel=G,r.isNumber=B,r.isPostalCA=re,r.isTime=q,r.isType=u,r.isUrl=Q,r.isZip=ee,r.momentToFPFormat=g,r.monthToNumber=f,r.normalizeValidationResult=ie,r.parseColor=ne,r.parseDate=w,r.parseDateTime=S,r.parseDateTimeToString=V,r.parseDateToString=Y,r.parseInteger=j,r.parseNANPTel=z,r.parseNumber=K,r.parsePostalCA=te,r.parseTime=I,r.parseTimeToString=$,r.parseUrl=J,r.parseZip=X,r.yearToFull=T,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})})})(E,E.exports);var o=E.exports;class H extends Event{constructor(r){super("validationSuccess",{cancelable:!0});m(this,"submitEvent");this.submitEvent=r}}class O extends Event{constructor(r){super("validationError",{cancelable:!0});m(this,"submitEvent");this.submitEvent=r}}class k{constructor(t,r={}){m(this,"form");m(this,"inputs",[]);m(this,"inputErrors",{});m(this,"messages",{ERROR_MAIN:"There is a problem with your submission.",ERROR_GENERIC:"Enter a valid value.",ERROR_REQUIRED:"This field is required.",OPTION_REQUIRED:"An option must be selected.",CHECKED_REQUIRED:"This must be checked.",ERROR_MAXLENGTH:"This must be ${val} characters or fewer.",ERROR_MINLENGTH:"This must be at least ${val} characters.",ERROR_NUMBER:"This must be a number.",ERROR_INTEGER:"This must be a whole number.",ERROR_TEL:"This is not a valid telephone number.",ERROR_EMAIL:"This is not a valid email address.",ERROR_ZIP:"This is not a valid zip code.",ERROR_POSTAL:"This is not a valid postal code.",ERROR_DATE:"This is not a valid date.",ERROR_DATE_PAST:"The date must be in the past.",ERROR_DATE_FUTURE:"The date must be in the future.",ERROR_DATE_RANGE:"The date is outside the allowed range.",ERROR_DATETIME:"This is not a valid date and time.",ERROR_TIME:"This is not a valid time.",ERROR_TIME_RANGE:"The time is outside the allowed range.",ERROR_URL:"This is not a valid URL.",ERROR_COLOR:"This is not a valid CSS colour.",ERROR_CUSTOM_VALIDATION:"There was a problem validating this field."});m(this,"debug");m(this,"autoInit");m(this,"preventSubmit",!1);m(this,"hiddenClasses");m(this,"errorMainClasses");m(this,"errorInputClasses");m(this,"dispatchTimeout",0);m(this,"originalNoValidate",!1);m(this,"validationSuccessCallback");m(this,"validationErrorCallback");m(this,"submitHandlerRef",this.submitHandler.bind(this));m(this,"inputInputHandlerRef",this.inputInputHandler.bind(this));m(this,"inputChangeHandlerRef",this.inputChangeHandler.bind(this));m(this,"inputKeydownHandlerRef",this.inputKeydownHandler.bind(this));m(this,"inputHandlers",{number:{parse:o.parseNumber,isValid:o.isNumber,error:this.messages.ERROR_NUMBER},integer:{parse:o.parseInteger,isValid:o.isInteger,error:this.messages.ERROR_INTEGER},tel:{parse:o.parseNANPTel,isValid:o.isNANPTel,error:this.messages.ERROR_TEL},email:{parse:t=>t.trim(),isValid:o.isEmail,error:this.messages.ERROR_EMAIL},zip:{parse:o.parseZip,isValid:o.isZip,error:this.messages.ERROR_ZIP},postal:{parse:o.parsePostalCA,isValid:o.isPostalCA,error:this.messages.ERROR_POSTAL},url:{parse:o.parseUrl,isValid:o.isUrl,error:this.messages.ERROR_URL},date:{parse:o.parseDateToString,isValid:o.isDate,error:this.messages.ERROR_DATE},datetime:{parse:o.parseDateTimeToString,isValid:o.isDateTime,error:this.messages.ERROR_DATETIME},time:{parse:o.parseTimeToString,isValid:o.isTime,error:this.messages.ERROR_TIME},color:{parse:t=>t.trim().toLowerCase(),isValid:o.isColor,error:this.messages.ERROR_COLOR}});m(this,"isSubmitting",!1);if(!t)throw new Error("Validator requires a form to be passed as the first argument.");if(!(t instanceof HTMLFormElement))throw new Error("form argument must be an instance of HTMLFormElement");this.form=t,(t.dataset.preventSubmit===""||t.dataset.preventSubmit)&&(this.preventSubmit=!0),Object.assign(this.messages,r.messages||{}),this.debug=r.debug||!1,this.autoInit=r.autoInit!==!1,this.preventSubmit=r.preventSubmit!==void 0?r.preventSubmit:this.preventSubmit,this.hiddenClasses=r.hiddenClasses||"hidden opacity-0",this.errorMainClasses=r.errorMainClasses||"m-2 border border-red-500 bg-red-100 p-3 dark:bg-red-900/80 text-center",this.errorInputClasses=r.errorInputClasses||"border-red-600 dark:border-red-500",this.validationSuccessCallback=r.validationSuccessCallback||(()=>{}),this.validationErrorCallback=r.validationErrorCallback||(()=>{}),this.autoInit&&this.init(),new MutationObserver(()=>this.autoInit&&this.init()).observe(t,{childList:!0})}addEventListeners(){this.form.addEventListener("submit",this.submitHandlerRef),this.form.addEventListener("input",this.inputInputHandlerRef),this.form.addEventListener("change",this.inputChangeHandlerRef),this.form.addEventListener("keydown",this.inputKeydownHandlerRef),this.form.addEventListener("remove",this.destroy,{once:!0})}removeEventListeners(){this.form.removeEventListener("submit",this.submitHandlerRef),this.form.removeEventListener("input",this.inputInputHandlerRef),this.form.removeEventListener("change",this.inputChangeHandlerRef),this.form.removeEventListener("keydown",this.inputKeydownHandlerRef),this.form.removeEventListener("remove",this.destroy)}init(){this.inputs=Array.from(this.form.elements).filter(t=>t instanceof HTMLInputElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement),this.inputs.forEach(t=>{!t.name&&!t.id&&(t.id=`vl-input-${Math.random().toString(36).slice(2)}`),this.inputErrors[t.name||t.id]=[]}),this.originalNoValidate=this.form.hasAttribute("novalidate"),this.form.setAttribute("novalidate","novalidate"),this.removeEventListeners(),this.addEventListeners()}getErrorEl(t){const r=f=>{const T=f.replace(/([.#{}()\\?*[\]-])/g,"\\$1");return this.form.querySelector(`#${T}`)||document.getElementById(f)||null},n=t.getAttribute("aria-describedby");if(n){const f=r(n);if(f)return f}const u=r(t.id+"-error");return u||r(t.name+"-error")||null}addErrorMain(t){const r=document.createElement("div");r.id="form-error-main",this.errorMainClasses.split(" ").forEach(n=>{r.classList.add(n)}),t?r.innerHTML=t:r.innerHTML=this.messages.ERROR_MAIN,this.form.appendChild(r)}addInputError(t,r=t.dataset.errorDefault||this.messages.ERROR_GENERIC){const n=t.name||t.id;this.debug&&console.log("Invalid value for "+n+": "+r),n in this.inputErrors||(this.inputErrors[n]=[]),this.inputErrors[n].includes(r)||this.inputErrors[n].push(r)}showInputErrors(t){if(!t||!t.name&&!t.id)return;const r=t.name||t.id,n=r in this.inputErrors?this.inputErrors[r]:[];if(!n.length)return;t.setAttribute("aria-invalid","true"),this.errorInputClasses.split(" ").forEach(g=>{t.classList.add(g)});let u=this.getErrorEl(t);u&&(u.innerHTML=n.join("<br>"),this.hiddenClasses.split(" ").forEach(g=>{u&&u.classList.remove(g)}))}showFormErrors(){if(this.inputs.forEach(t=>this.showInputErrors(t)),Object.values(this.inputErrors).some(t=>Array.isArray(t)&&t.length)){const t=this.form.querySelectorAll("#form-error-main");t.length?t.forEach(r=>{r.innerHTML||(r.innerHTML=this.messages.ERROR_MAIN),this.hiddenClasses.split(" ").forEach(n=>{r.classList.remove(n)})}):this.addErrorMain()}}clearInputErrors(t){this.inputErrors[t.name||t.id]=[],t.removeAttribute("aria-invalid");let r=this.getErrorEl(t);r&&(this.errorInputClasses.split(" ").forEach(n=>{t.classList.remove(n)}),this.hiddenClasses.split(" ").forEach(n=>{r&&r.classList.add(n)}),r.textContent="")}clearFormErrors(){this.form.querySelectorAll("#form-error-main").forEach(t=>{this.hiddenClasses.split(" ").forEach(r=>{t.classList.add(r)})}),this.inputs.forEach(t=>this.clearInputErrors(t))}validateRequired(t){let r=!0;if(t.required&&(t.value===""||t instanceof HTMLInputElement&&["checkbox","radio"].includes(t.type)&&!t.checked))if(t instanceof HTMLInputElement&&["checkbox","radio"].includes(t.type)){let n=!1,u=t.name;const g=this.form.querySelectorAll(`input[name="${u}"]`);if(g.forEach(f=>{if(f instanceof HTMLInputElement&&f.checked===!0){n=!0;return}}),n===!1){r=!1;let f=g.length>1?this.messages.OPTION_REQUIRED:this.messages.CHECKED_REQUIRED;t.dataset.errorDefault&&(f=t.dataset.errorDefault),this.addInputError(t,f)}}else o.isFormControl(t)&&(r=!1,this.addInputError(t,t.dataset.errorDefault||this.messages.ERROR_REQUIRED));return r}validateLength(t){let r=!0;if(t.disabled)return r;if((t instanceof HTMLInputElement||t instanceof HTMLTextAreaElement)&&t.value.length){let n=t.minLength>0?t.minLength:t.dataset.minLength?parseInt(t.dataset.minLength):0,u=t.maxLength>0&&t.maxLength<5e5?t.maxLength:t.dataset.maxLength?parseInt(t.dataset.maxLength):1/0;n>0&&t.value.length<n&&(r=!1,this.addInputError(t,this.messages.ERROR_MINLENGTH.replace("${val}",n.toString()))),t.value.length>u&&(r=!1,this.addInputError(t,this.messages.ERROR_MAXLENGTH.replace("${val}",u.toString())))}return r}validateInputType(t){const r=t.dataset.type||t.type,n=this.inputHandlers[t.type]||this.inputHandlers[r];if(n){const u=t.dataset.dateFormat||t.dataset.timeFormat,g=n.parse(t.value,u),f=["date","time","datetime-local","month","week"];if(g.length&&!f.includes(t.type)&&(t.value=g),!n.isValid(t.value))return this.addInputError(t,n.error),!1}return!0}validateDateRange(t){if(t.dataset.dateRange){const r=t.dataset.dateRange,n=o.parseDate(t.value);if(!isNaN(n.getTime())&&!o.isDateInRange(n,r)){let u=t.dataset.errorDefault||this.messages.ERROR_DATE_RANGE;return r==="past"?u=this.messages.ERROR_DATE_PAST:r==="future"&&(u=this.messages.ERROR_DATE_FUTURE),this.addInputError(t,u),!1}}return!0}validatePattern(t){const r=t.dataset.pattern||t instanceof HTMLInputElement&&t.pattern||null;return r&&!new RegExp(r).test(t.value)?(this.addInputError(t),!1):!0}async validateCustom(t){if(t.disabled)return!0;const r=t.dataset.validation;if(!r||typeof r!="string")return!0;const n=window[r];if(!n||typeof n!="function")return!0;let u;try{u=await Promise.resolve(n(t.value)),u=o.normalizeValidationResult(u)}catch{return this.addInputError(t,this.messages.ERROR_CUSTOM_VALIDATION),!1}const g=u.messages.join("<br>")||this.messages.ERROR_CUSTOM_VALIDATION;return u.valid||this.addInputError(t,g),u.valid}async validateInput(t){if(!(t instanceof HTMLInputElement)||!t.value.length)return!0;let r=!0;return t.disabled||(r=this.validateInputType(t)&&r,r=this.validateDateRange(t)&&r,r=this.validatePattern(t)&&r,r=await this.validateCustom(t)&&r),r}async validate(t){let r=!0;for(const n of this.inputs)n.disabled||(r=this.validateRequired(n)&&r,r=this.validateLength(n)&&r,r=await this.validateInput(n)&&r,n.value.length||(r=await this.validateCustom(n)&&r));return r}async submitHandler(t){if(this.isSubmitting)return;t.preventDefault(),this.clearFormErrors();let r=await this.validate(t);this.showFormErrors();const n=new H(t),u=new O(t);r?(this.form.dispatchEvent(n),this.validationSuccessCallback&&this.validationSuccessCallback(t)):(this.form.dispatchEvent(u),this.validationErrorCallback&&this.validationErrorCallback(t)),r&&!this.preventSubmit&&(this.isSubmitting=!0,n.defaultPrevented||this.form.submit(),this.isSubmitting=!1)}shouldSkipValidation(t){return typeof t.dataset.novalidate>"u"?!1:t.dataset.novalidate===""?!0:t.dataset.novalidate==="true"}async inputChangeHandler(t){!(t.target instanceof HTMLInputElement)||this.shouldSkipValidation(t.target)||(this.clearInputErrors(t.target),this.validateLength(t.target),await this.validateInput(t.target),this.showInputErrors(t.target))}inputInputHandler(t){const r=t.target;this.shouldSkipValidation(r)||(o.isType(r,"integer")&&(r.value=o.parseInteger(r.value)),r.type!=="number"&&o.isType(r,["number","float","decimal"])&&(r.value=o.parseNumber(r.value)),o.isType(r,"color")&&this.syncColorInput(t))}syncColorInput(t){let r=t.target,n=r;r.type==="color"&&(n=this.form.querySelector(`#${r.id.replace(/-color/,"")}`));let u=this.form.querySelector(`#${n.id}-color-label`);if((r.dataset.type||"")==="color"){let g=this.form.querySelector(`input#${r.id}-color`);if(!g||!o.isColor(r.value))return;g.value=o.parseColor(r.value)}r.type==="color"&&(n.value=r.value),u&&(u.style.backgroundColor=r.value),clearTimeout(this.dispatchTimeout),this.dispatchTimeout=window.setTimeout(()=>{n.dispatchEvent(new Event("change",{bubbles:!0}))},200)}inputKeydownHandler(t){t.target instanceof HTMLInputElement&&o.isType(t.target,"integer")&&(t.key==="ArrowUp"?(t.preventDefault(),t.target.value===""&&(t.target.value="0"),t.target.value=(parseInt(t.target.value)+1).toString()):t.key==="ArrowDown"&&(parseInt(t.target.value)>0?t.target.value=(parseInt(t.target.value)-1).toString():t.target.value="0"))}destroy(){this.removeEventListeners(),this.originalNoValidate||this.form.removeAttribute("novalidate")}}return k});
|
|
1
|
+
(function(y,R){typeof exports=="object"&&typeof module<"u"?module.exports=R():typeof define=="function"&&define.amd?define(R):(y=typeof globalThis<"u"?globalThis:y||self,y.Validator=R())})(this,function(){"use strict";var le=Object.defineProperty;var de=(y,R,o)=>R in y?le(y,R,{enumerable:!0,configurable:!0,writable:!0,value:o}):y[R]=o;var p=(y,R,o)=>(de(y,typeof R!="symbol"?R+"":R,o),o);var y=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},R={exports:{}};(function(L,t){(function(r,n){n(t)})(y,function(r){function n(e){return e instanceof HTMLInputElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement}function l(e,a){typeof a=="string"&&(a=[a]);const s=e.dataset.type||"",i=e.type;return!!(a.includes(s)||a.includes(i))}function g(e){return e.replace(/YYYY/g,"Y").replace(/YY/g,"y").replace(/MMMM/g,"F").replace(/MMM/g,"{3}").replace(/MM/g,"{2}").replace(/M/g,"n").replace(/DD/g,"{5}").replace(/D/g,"j").replace(/dddd/g,"l").replace(/ddd/g,"D").replace(/dd/g,"D").replace(/d/g,"w").replace(/HH/g,"{6}").replace(/H/g,"G").replace(/hh/g,"h").replace(/mm/g,"i").replace(/m/g,"i").replace(/ss/g,"S").replace(/s/g,"s").replace(/A/gi,"K").replace(/\{3\}/g,"M").replace(/\{2\}/g,"m").replace(/\{5\}/g,"d").replace(/\{6\}/g,"H")}function E(e){const a=parseInt(e);if(typeof e=="number"||!isNaN(a))return a-1;const s=new Date(`1 ${e} 2000`).getMonth();if(!isNaN(s))return s;const i={ja:0,en:0,fe:1,fé:1,ap:3,ab:3,av:3,mai:4,juin:5,juil:6,au:7,ag:7,ao:7,se:8,o:9,n:10,d:11};for(const h in i)if(e.toLowerCase().startsWith(h))return i[h];throw new Error("Invalid month name: "+e)}function T(e){return typeof e=="string"&&(e=parseInt(e.replace(/\D/g,""))),e>99?e:e<(new Date().getFullYear()+20)%100?e+2e3:e+1900}function w(e){if(e instanceof Date)return e;e=e.trim().toLowerCase();let a=0,s=0,i=0,h=0,d=0,u=0;const m=new RegExp(/\d{1,2}\:\d\d(?:\:\d\ds?)?\s?(?:[a|p]m?)?/gi);if(m.test(e)){const v=e.match(m)[0];e=e.replace(v,"").trim();const b=I(v);if(b!==null&&({hour:h,minute:d,second:u}=b),e.length<=2){const C=new Date;return new Date(C.getFullYear(),C.getMonth(),C.getDate(),h,d,u)}}const f=/(^|\b)(mo|tu|we|th|fr|sa|su|lu|mard|mer|jeu|ve|dom)[\w]*\.?/gi;e=e.replace(f,"").trim();const c=new Date(new Date().setHours(0,0,0,0));if(/(now|today)/.test(e))return c;if(e.includes("tomorrow"))return new Date(c.setDate(c.getDate()+1));e.length===8&&(e=e.replace(/(\d\d\d\d)(\d\d)(\d\d)/,"$1-$2-$3")),e.length===6&&(e=e.replace(/(\d\d)(\d\d)(\d\d)/,T(e.slice(0,2))+"-$2-$3"));try{({year:a,month:s,day:i}=Y(e))}catch{return new Date("")}return new Date(a,s-1,i,h,d,u)}function $(e,a=[null,null,null]){const s=i=>i.filter(h=>!a.includes(h));return e===0||e>31?s(["y"]):e>12?s(["d","y"]):e>=1&&e<=12?s(["m","d","y"]):[]}function Y(e){const a=e.split(/[\s-/:.,]+/).filter(d=>d!=="");if(a.length<3){if(e.match(/\d{4}/)!==null)throw new Error("Invalid Date");a.unshift(String(new Date().getFullYear()))}const s={year:0,month:0,day:0};function i(d,u){d==="year"?s.year=T(u):s[d]=u}let h=0;for(;!(s.year&&s.month&&s.day);){e:for(const d of a){if(h++,/^[a-zA-Zé]+$/.test(d)){s.month||i("month",E(d)+1);continue}if(/^'\d\d$/.test(d)||/^\d{3,5}$/.test(d)){s.year||i("year",parseInt(d.replace(/'/,"")));continue}const u=parseInt(d);if(isNaN(u))throw console.error(`not date because ${d} isNaN`),new Error("Invalid Date");const m=$(u,[s.year?"y":null,s.month?"m":null,s.day?"d":null]);if(m.length==1){if(m[0]==="m"&&!s.month){i("month",u);continue e}if(m[0]==="d"&&!s.day){i("day",u);continue e}if(m[0]==="y"&&!s.year){i("year",u);continue e}}h>3&&(!s.month&&m.includes("m")?i("month",u):!s.day&&m.includes("d")&&i("day",u))}if(h>6)throw new Error("Invalid Date")}if(s.year&&s.month&&s.day)return s;throw new Error("Invalid Date")}function I(e){if(e=e.trim().toLowerCase(),e==="now"){const c=new Date;return{hour:c.getHours(),minute:c.getMinutes(),second:c.getSeconds()}}if(e==="noon")return{hour:12,minute:0,second:0};const a=e.match(/(\d{3,4})/);if(a){const c=a[1].length,v=a[1].slice(0,c==3?1:2),b=a[1].slice(-2);e=e.replace(a[1],v+":"+b)}const s=new RegExp(/^(\d{1,2})(?::(\d{1,2}))?\s*(?:(a|p)\.?m?\.?)?$/i);if(s.test(e)){const c=e.match(s);if(c===null)return null;e=c[1]+":"+(c[2]||"00")+(c[3]||"")}const i=new RegExp(/^(\d{1,2}):(\d{1,2})(?::(\d{1,2}))?\s*(?:(a|p)m?)?$/i);if(!i.test(e))return null;const h=e.match(i);if(h===null)return null;const d=parseInt(h[1]),u=parseInt(h[2]),m=h[3]?parseInt(h[3]):0,f=h[4];return isNaN(d)||isNaN(u)||isNaN(m)?null:f==="p"&&d<12?{hour:d+12,minute:u,second:m}:f==="a"&&d===12?{hour:0,minute:u,second:m}:d<0||d>23||u<0||u>59||m<0||m>59?null:{hour:d,minute:u,second:m}}function V(e,a="h:mm A"){const s=I(e);if(s){const i=new Date;return i.setHours(s.hour),i.setMinutes(s.minute),i.setSeconds(s.second),i.setMilliseconds(0),M(i,a)}return""}function S(e){if(e instanceof Date)return e;if(e.trim().length<3)return null;e=e.replace(/(\d)T(\d)/,"$1 $2");let a=e.split(/[\s,]+/).filter(f=>f!==""),s="",i="",h="";a.forEach((f,c)=>{const v=f.match(/^(\d{1,4})([apAP]\.?[mM]?\.?)/);v?i=v[0]:(f.includes(":")||f==="now"||f==="noon")&&(i=f),H(f)&&(h=f,!i&&c>0&&O(a[c-1])&&(i=a[c-1]))}),i?a=a.filter(f=>f!==i&&f!==h):[s,a]=P(a);const d=i?`${i} ${h}`:s?a.join(" "):"",u=I(d)||{hour:0,minute:0,second:0},m=w(s||a.join(" ").trim()||"today");return!m||isNaN(m.getTime())?null:new Date(m.getFullYear(),m.getMonth(),m.getDate(),u.hour,u.minute,u.second)}function P(e){for(let a=3;a>0;a--){const s=e.slice(0,a).join(" ");if(N(s))return[s,e.slice(a)]}return["",e]}function M(e,a="YYYY-MM-DD"){if(e=w(e),isNaN(e.getTime()))return"";const s={y:e.getFullYear(),M:e.getMonth(),D:e.getDate(),W:e.getDay(),H:e.getHours(),m:e.getMinutes(),s:e.getSeconds(),ms:e.getMilliseconds()},i=(c,v=2)=>(c+"").padStart(v,"0"),h=()=>s.H%12||12,d=c=>c<12?"AM":"PM",u=c=>"January|February|March|April|May|June|July|August|September|October|November|December".split("|")[c];function m(c,v=0){const b="Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday".split("|");return v?b[c].slice(0,v):b[c]}const f={YY:String(s.y).slice(-2),YYYY:s.y,M:s.M+1,MM:i(s.M+1),MMMM:u(s.M),MMM:u(s.M).slice(0,3),D:String(s.D),DD:i(s.D),d:String(s.W),dd:m(s.W,2),ddd:m(s.W,3),dddd:m(s.W),H:String(s.H),HH:i(s.H),h:h(),hh:i(h()),A:d(s.H),a:d(s.H).toLowerCase(),m:String(s.m),mm:i(s.m),s:String(s.s),ss:i(s.s),SSS:i(s.ms,3)};return a.replace(/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,(c,v)=>v||f[c])}function U(e,a){const s=w(e);return isNaN(s.getTime())?"":((!a||a.length===0)&&(a="YYYY-MMM-DD"),M(s,a))}function F(e,a){const s=S(e);return s===null||isNaN(s.getTime())?"":((!a||a.length===0)&&(a="YYYY-MMM-DD h:mm A"),M(s,a))}function N(e){if(typeof e!="string"&&!(e instanceof Date))return!1;let a=w(e);return a==null?!1:!isNaN(a.getTime())}function q(e){if(typeof e!="string"&&!(e instanceof Date))return!1;let a=S(e);return a==null?!1:!isNaN(a.getTime())}function Z(e,a){return!(a==="past"&&e>new Date||a==="future"&&e.getTime()<new Date().setHours(0,0,0,0))}function H(e){const a=e.toLowerCase().replace(/[.\s]/g,"");return["am","pm","a","p"].includes(a)}function O(e){let a=I(e);return a===null?!1:!isNaN(a.hour)&&!isNaN(a.minute)&&!isNaN(a.second)}function j(e){if(e.length>255||!new RegExp(/^.+@.+\.[a-zA-Z0-9]{2,}$/).test(e))return!1;let a="";return a+="^([a-zA-Z0-9!#$%'*+/=?^_`{|}~-]+",a+="(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*",a+="|",a+='"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*"',a+=")@(",a+="(",a+="(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+",a+="[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?",a+=")",a+=")$",new RegExp(a).test(e)}function z(e){return e=e.replace(/^[^2-90]+/g,""),e=e.replace(/(\d\d\d).*?(\d\d\d).*?(\d\d\d\d)(.*)/,"$1-$2-$3$4"),e}function G(e){return/^\d\d\d-\d\d\d-\d\d\d\d$/.test(e)}function B(e){return e.replace(/[^0-9]/g,"")}function K(e){return/^\-?\d*\.?\d*$/.test(e)}function W(e){return e.replace(/[^\-0-9.]/g,"").replace(/(^-)|(-)/g,(a,s)=>s?"-":"").replace(/(\..*)\./g,"$1")}function J(e){return/^\-?\d*$/.test(e)}function Q(e){return e=e.trim(),new RegExp("^(?:[a-z+]+:)?//","i").test(e)?e:"https://"+e}function X(e){return new RegExp("^(?:[-a-z+]+:)?//","i").test(e)}function ee(e){return e=e.replace(/[^0-9]/g,"").replace(/(.{5})(.*)/,"$1-$2").trim(),e.length===6&&(e=e.replace(/-/,"")),e}function te(e){return new RegExp(/^\d{5}(-\d{4})?$/).test(e)}function re(e){return e=e.toUpperCase().replace(/[^A-Z0-9]/g,"").replace(/(.{3})\s*(.*)/,"$1 $2").trim(),e}function ae(e){return new RegExp(/^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$/).test(e)}function se(e){return["transparent","currentColor"].includes(e)?!0:typeof e!="string"||!e.trim()?!1:typeof CSS=="object"&&typeof CSS.supports=="function"?CSS.supports("color",e):ne(e)}function ne(e){const a=new RegExp(/^rgba?\(\s*(\d{1,3}%?,\s*){2}\d{1,3}%?\s*(?:,\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),s=new RegExp(/^hsla?\(\s*\d+(deg|grad|rad|turn)?,\s*\d{1,3}%,\s*\s*\d{1,3}%(?:,\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),i=new RegExp(/^rgba?\(\s*(\d{1,3}%?\s+){2}\d{1,3}%?\s*(?:\s*\/\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),h=new RegExp(/^hsla?\(\s*\d+(deg|grad|rad|turn)?\s+\d{1,3}%\s+\s*\d{1,3}%(?:\s*\/\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),d=new RegExp(/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i);let u="aliceblue|antiquewhite|aqua|aquamarine|azure|beige|bisque|black|blanchedalmond|blue|blueviolet|brown|burlywood|cadetblue|chartreuse|chocolate|coral|cornflowerblue|cornsilk|crimson|cyan|darkblue|darkcyan|darkgoldenrod|darkgray|darkgreen|darkgrey|darkkhaki|darkmagenta|darkolivegreen|darkorange|darkorchid|darkred|darksalmon|darkseagreen|darkslateblue|darkslategray|darkslategrey|darkturquoise|darkviolet|deeppink|deepskyblue|dimgray|dimgrey|dodgerblue|firebrick|floralwhite|forestgreen|fuchsia|gainsboro|ghostwhite|gold|goldenrod|gray|green|greenyellow|grey|honeydew|hotpink|indianred|indigo|ivory|khaki|lavender|lavenderblush|lawngreen|lemonchiffon|lightblue|lightcoral|lightcyan|lightgoldenrodyellow|lightgray|lightgreen|lightgrey|lightpink|lightsalmon|lightseagreen|lightskyblue|lightslategray|lightslategrey|lightsteelblue|lightyellow|lime|limegreen|linen|magenta|maroon|mediumaquamarine|mediumblue|mediumorchid|mediumpurple|mediumseagreen|mediumslateblue|mediumspringgreen|mediumturquoise|mediumvioletred|midnightblue|mintcream|mistyrose|moccasin|navajowhite|navy|oldlace|olive|olivedrab|orange|orangered|orchid|palegoldenrod|palegreen|paleturquoise|palevioletred|papayawhip|peachpuff|peru|pink|plum|powderblue|purple|rebeccapurple|red|rosybrown|royalblue|saddlebrown|salmon|sandybrown|seagreen|seashell|sienna|silver|skyblue|slateblue|slategray|slategrey|snow|springgreen|steelblue|tan|teal|thistle|tomato|turquoise|violet|wheat|white|whitesmoke|yellow|yellowgreen";const m=new RegExp(`^(${u})$`,"i");return a.test(e)||s.test(e)||i.test(e)||h.test(e)||d.test(e)||m.test(e)}let D=null;const A=new Map;function ie(e){if(e=e.trim().toLowerCase(),["transparent","currentcolor"].includes(e))return e;if(A.has(e))return A.get(e);D===null&&(D=document.createElement("canvas"),D.willReadFrequently=!0);let a=D.getContext("2d");if(!a)throw new Error("Can't get context from colorCanvas");a.fillStyle=e,a.fillRect(0,0,1,1);let s=a.getImageData(0,0,1,1).data,i="#"+("000000"+(s[0]<<16|s[1]<<8|s[2]).toString(16)).slice(-6);return A.set(e,i),i}function oe(e){let a={valid:!1,error:!1,messages:[]};return typeof e=="boolean"?{valid:e,error:!1,messages:[]}:typeof e=="string"?{valid:!1,error:!1,messages:[e]}:(typeof e.valid=="boolean"&&(a.valid=e.valid),typeof e.message=="string"&&(a.messages=[e.message]),typeof e.messages=="string"&&(a.messages=[e.messages]),Array.isArray(e.messages)&&(a.messages=e.messages),e.error===!0&&(a.error=!0),a)}r.formatDateTime=M,r.isColor=se,r.isDate=N,r.isDateInRange=Z,r.isDateTime=q,r.isEmail=j,r.isFormControl=n,r.isInteger=J,r.isMeridiem=H,r.isNANPTel=G,r.isNumber=K,r.isPostalCA=ae,r.isTime=O,r.isType=l,r.isUrl=X,r.isZip=te,r.momentToFPFormat=g,r.monthToNumber=E,r.normalizeValidationResult=oe,r.parseColor=ie,r.parseDate=w,r.parseDateTime=S,r.parseDateTimeToString=F,r.parseDateToString=U,r.parseInteger=B,r.parseNANPTel=z,r.parseNumber=W,r.parsePostalCA=re,r.parseTime=I,r.parseTimeToString=V,r.parseUrl=Q,r.parseZip=ee,r.yearToFull=T,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})})})(R,R.exports);var o=R.exports;class k extends Event{constructor(r){super("validationSuccess",{cancelable:!0});p(this,"submitEvent");this.submitEvent=r}}class _ extends Event{constructor(r){super("validationError",{cancelable:!0});p(this,"submitEvent");this.submitEvent=r}}class x{constructor(t,r={}){p(this,"form");p(this,"inputs",[]);p(this,"inputErrors",{});p(this,"messages",{ERROR_MAIN:"There is a problem with your submission.",ERROR_GENERIC:"Enter a valid value.",ERROR_REQUIRED:"This field is required.",OPTION_REQUIRED:"An option must be selected.",CHECKED_REQUIRED:"This must be checked.",ERROR_MAXLENGTH:"This must be ${val} characters or fewer.",ERROR_MINLENGTH:"This must be at least ${val} characters.",ERROR_NUMBER:"This must be a number.",ERROR_INTEGER:"This must be a whole number.",ERROR_TEL:"This is not a valid telephone number.",ERROR_EMAIL:"This is not a valid email address.",ERROR_ZIP:"This is not a valid zip code.",ERROR_POSTAL:"This is not a valid postal code.",ERROR_DATE:"This is not a valid date.",ERROR_DATE_PAST:"The date must be in the past.",ERROR_DATE_FUTURE:"The date must be in the future.",ERROR_DATE_RANGE:"The date is outside the allowed range.",ERROR_DATETIME:"This is not a valid date and time.",ERROR_TIME:"This is not a valid time.",ERROR_TIME_RANGE:"The time is outside the allowed range.",ERROR_URL:"This is not a valid URL.",ERROR_COLOR:"This is not a valid CSS colour.",ERROR_CUSTOM_VALIDATION:"There was a problem validating this field."});p(this,"debug");p(this,"autoInit");p(this,"preventSubmit",!1);p(this,"hiddenClasses");p(this,"errorMainClasses");p(this,"errorInputClasses");p(this,"dispatchTimeout",0);p(this,"timeoutId",0);p(this,"debouncedInit");p(this,"originalNoValidate",!1);p(this,"validationSuccessCallback");p(this,"validationErrorCallback");p(this,"submitHandlerRef",this.submitHandler.bind(this));p(this,"inputInputHandlerRef",this.inputInputHandler.bind(this));p(this,"inputChangeHandlerRef",this.inputChangeHandler.bind(this));p(this,"inputKeydownHandlerRef",this.inputKeydownHandler.bind(this));p(this,"inputHandlers",{number:{parse:o.parseNumber,isValid:o.isNumber,error:this.messages.ERROR_NUMBER},integer:{parse:o.parseInteger,isValid:o.isInteger,error:this.messages.ERROR_INTEGER},tel:{parse:o.parseNANPTel,isValid:o.isNANPTel,error:this.messages.ERROR_TEL},email:{parse:t=>t.trim(),isValid:o.isEmail,error:this.messages.ERROR_EMAIL},zip:{parse:o.parseZip,isValid:o.isZip,error:this.messages.ERROR_ZIP},postal:{parse:o.parsePostalCA,isValid:o.isPostalCA,error:this.messages.ERROR_POSTAL},url:{parse:o.parseUrl,isValid:o.isUrl,error:this.messages.ERROR_URL},date:{parse:o.parseDateToString,isValid:o.isDate,error:this.messages.ERROR_DATE},datetime:{parse:o.parseDateTimeToString,isValid:o.isDateTime,error:this.messages.ERROR_DATETIME},time:{parse:o.parseTimeToString,isValid:o.isTime,error:this.messages.ERROR_TIME},color:{parse:t=>t.trim().toLowerCase(),isValid:o.isColor,error:this.messages.ERROR_COLOR}});p(this,"isSubmitting",!1);if(!t)throw new Error("Validator requires a form to be passed as the first argument.");if(!(t instanceof HTMLFormElement))throw new Error("form argument must be an instance of HTMLFormElement");this.form=t,(t.dataset.preventSubmit===""||t.dataset.preventSubmit)&&(this.preventSubmit=!0),Object.assign(this.messages,r.messages||{}),this.debug=r.debug||!1,this.autoInit=r.autoInit!==!1,this.preventSubmit=r.preventSubmit!==void 0?r.preventSubmit:this.preventSubmit,this.hiddenClasses=r.hiddenClasses||"hidden opacity-0",this.errorMainClasses=r.errorMainClasses||"m-2 border border-red-500 bg-red-100 p-3 dark:bg-red-900/80 text-center",this.errorInputClasses=r.errorInputClasses||"border-red-600 dark:border-red-500",this.validationSuccessCallback=r.validationSuccessCallback||(()=>{}),this.validationErrorCallback=r.validationErrorCallback||(()=>{}),this.debouncedInit=this.debounce(this.init.bind(this),45),this.autoInit&&this.init(),new MutationObserver(()=>this.autoInit&&this.debouncedInit()).observe(t,{childList:!0})}debounce(t,r){return(...n)=>{clearTimeout(this.timeoutId),this.timeoutId=window.setTimeout(()=>t(...n),r)}}addEventListeners(){this.form.addEventListener("submit",this.submitHandlerRef),this.form.addEventListener("input",this.inputInputHandlerRef),this.form.addEventListener("change",this.inputChangeHandlerRef),this.form.addEventListener("keydown",this.inputKeydownHandlerRef),this.form.addEventListener("remove",this.destroy,{once:!0})}removeEventListeners(){this.form.removeEventListener("submit",this.submitHandlerRef),this.form.removeEventListener("input",this.inputInputHandlerRef),this.form.removeEventListener("change",this.inputChangeHandlerRef),this.form.removeEventListener("keydown",this.inputKeydownHandlerRef),this.form.removeEventListener("remove",this.destroy)}init(){this.inputs=Array.from(this.form.elements).filter(t=>t instanceof HTMLInputElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement),this.inputs.forEach(t=>{!t.name&&!t.id&&(t.id=`vl-input-${Math.random().toString(36).slice(2)}`),this.inputErrors[t.name||t.id]=[]}),this.originalNoValidate=this.form.hasAttribute("novalidate"),this.form.setAttribute("novalidate","novalidate"),this.removeEventListeners(),this.addEventListeners()}getErrorEl(t){const r=E=>{const T=E.replace(/([.#{}()\\?*[\]-])/g,"\\$1");return this.form.querySelector(`#${T}`)||document.getElementById(E)||null},n=t.getAttribute("aria-describedby");if(n){const E=r(n);if(E)return E}const l=r(t.id+"-error");return l||r(t.name+"-error")||null}addErrorMain(t){const r=document.createElement("div");r.id="form-error-main",this.errorMainClasses.split(" ").forEach(n=>{r.classList.add(n)}),t?r.innerHTML=t:r.innerHTML=this.messages.ERROR_MAIN,this.form.appendChild(r)}addInputError(t,r=t.dataset.errorDefault||this.messages.ERROR_GENERIC){const n=t.name||t.id;this.debug&&console.log("Invalid value for "+n+": "+r),n in this.inputErrors||(this.inputErrors[n]=[]),this.inputErrors[n].includes(r)||this.inputErrors[n].push(r)}showInputErrors(t){if(!t||!t.name&&!t.id)return;const r=t.name||t.id,n=r in this.inputErrors?this.inputErrors[r]:[];if(!n.length)return;t.setAttribute("aria-invalid","true"),this.errorInputClasses.split(" ").forEach(g=>{t.classList.add(g)});let l=this.getErrorEl(t);l&&(l.innerHTML=n.join("<br>"),this.hiddenClasses.split(" ").forEach(g=>{l&&l.classList.remove(g)}))}showFormErrors(){if(this.inputs.forEach(t=>this.showInputErrors(t)),Object.values(this.inputErrors).some(t=>Array.isArray(t)&&t.length)){const t=this.form.querySelectorAll("#form-error-main");t.length?t.forEach(r=>{r.innerHTML||(r.innerHTML=this.messages.ERROR_MAIN),this.hiddenClasses.split(" ").forEach(n=>{r.classList.remove(n)})}):this.addErrorMain()}}clearInputErrors(t){this.inputErrors[t.name||t.id]=[],t.removeAttribute("aria-invalid");let r=this.getErrorEl(t);r&&(this.errorInputClasses.split(" ").forEach(n=>{t.classList.remove(n)}),this.hiddenClasses.split(" ").forEach(n=>{r&&r.classList.add(n)}),r.textContent="")}clearFormErrors(){this.form.querySelectorAll("#form-error-main").forEach(t=>{this.hiddenClasses.split(" ").forEach(r=>{t.classList.add(r)})}),this.inputs.forEach(t=>this.clearInputErrors(t))}validateRequired(t){let r=!0;if(t.required&&(t.value===""||t instanceof HTMLInputElement&&["checkbox","radio"].includes(t.type)&&!t.checked))if(t instanceof HTMLInputElement&&["checkbox","radio"].includes(t.type)){let n=!1,l=t.name;const g=this.form.querySelectorAll(`input[name="${l}"]`);if(g.forEach(E=>{if(E instanceof HTMLInputElement&&E.checked===!0){n=!0;return}}),n===!1){r=!1;let E=g.length>1?this.messages.OPTION_REQUIRED:this.messages.CHECKED_REQUIRED;t.dataset.errorDefault&&(E=t.dataset.errorDefault),this.addInputError(t,E)}}else o.isFormControl(t)&&(r=!1,this.addInputError(t,t.dataset.errorDefault||this.messages.ERROR_REQUIRED));return r}validateLength(t){let r=!0;if(t.disabled)return r;if((t instanceof HTMLInputElement||t instanceof HTMLTextAreaElement)&&t.value.length){let n=t.minLength>0?t.minLength:t.dataset.minLength?parseInt(t.dataset.minLength):0,l=t.maxLength>0&&t.maxLength<5e5?t.maxLength:t.dataset.maxLength?parseInt(t.dataset.maxLength):1/0;n>0&&t.value.length<n&&(r=!1,this.addInputError(t,this.messages.ERROR_MINLENGTH.replace("${val}",n.toString()))),t.value.length>l&&(r=!1,this.addInputError(t,this.messages.ERROR_MAXLENGTH.replace("${val}",l.toString())))}return r}validateInputType(t){const r=t.dataset.type||t.type,n=this.inputHandlers[t.type]||this.inputHandlers[r];if(n){const l=t.dataset.dateFormat||t.dataset.timeFormat,g=n.parse(t.value,l),E=["date","time","datetime-local","month","week"];if(g.length&&!E.includes(t.type)&&(t.value=g),!n.isValid(t.value))return this.addInputError(t,n.error),!1}return!0}validateDateRange(t){if(t.dataset.dateRange){const r=t.dataset.dateRange,n=o.parseDate(t.value);if(!isNaN(n.getTime())&&!o.isDateInRange(n,r)){let l=t.dataset.errorDefault||this.messages.ERROR_DATE_RANGE;return r==="past"?l=this.messages.ERROR_DATE_PAST:r==="future"&&(l=this.messages.ERROR_DATE_FUTURE),this.addInputError(t,l),!1}}return!0}validatePattern(t){const r=t.dataset.pattern||t instanceof HTMLInputElement&&t.pattern||null;return r&&!new RegExp(r).test(t.value)?(this.addInputError(t),!1):!0}async validateCustom(t){if(t.disabled)return!0;const r=t.dataset.validation;if(!r||typeof r!="string")return!0;const n=window[r];if(!n||typeof n!="function")return!0;let l;try{l=await Promise.resolve(n(t.value)),l=o.normalizeValidationResult(l)}catch{return this.addInputError(t,this.messages.ERROR_CUSTOM_VALIDATION),!1}const g=l.messages.join("<br>")||this.messages.ERROR_CUSTOM_VALIDATION;return l.valid||this.addInputError(t,g),l.valid}async validateInput(t){if(!(t instanceof HTMLInputElement)||!t.value.length)return!0;let r=!0;return t.disabled||(r=this.validateInputType(t)&&r,r=this.validateDateRange(t)&&r,r=this.validatePattern(t)&&r,r=await this.validateCustom(t)&&r),r}async validate(t){let r=!0;for(const n of this.inputs)n.disabled||(r=this.validateRequired(n)&&r,r=this.validateLength(n)&&r,r=await this.validateInput(n)&&r,n.value.length||(r=await this.validateCustom(n)&&r));return r}async submitHandler(t){if(this.isSubmitting)return;t.preventDefault(),this.clearFormErrors();let r=await this.validate(t);this.showFormErrors();const n=new k(t),l=new _(t);r?(this.form.dispatchEvent(n),this.validationSuccessCallback&&this.validationSuccessCallback(t)):(this.form.dispatchEvent(l),this.validationErrorCallback&&this.validationErrorCallback(t)),r&&!this.preventSubmit&&(this.isSubmitting=!0,n.defaultPrevented||this.form.submit(),this.isSubmitting=!1)}shouldSkipValidation(t){return typeof t.dataset.novalidate>"u"?!1:t.dataset.novalidate===""?!0:t.dataset.novalidate==="true"}async inputChangeHandler(t){!(t.target instanceof HTMLInputElement)||this.shouldSkipValidation(t.target)||(this.clearInputErrors(t.target),this.validateLength(t.target),await this.validateInput(t.target),this.showInputErrors(t.target))}inputInputHandler(t){const r=t.target;this.shouldSkipValidation(r)||(o.isType(r,"integer")&&(r.value=o.parseInteger(r.value)),r.type!=="number"&&o.isType(r,["number","float","decimal"])&&(r.value=o.parseNumber(r.value)),o.isType(r,"color")&&this.syncColorInput(t))}syncColorInput(t){let r=t.target,n=r;r.type==="color"&&(n=this.form.querySelector(`#${r.id.replace(/-color/,"")}`));let l=this.form.querySelector(`#${n.id}-color-label`);if((r.dataset.type||"")==="color"){let g=this.form.querySelector(`input#${r.id}-color`);if(!g||!o.isColor(r.value))return;g.value=o.parseColor(r.value)}r.type==="color"&&(n.value=r.value),l&&(l.style.backgroundColor=r.value),clearTimeout(this.dispatchTimeout),this.dispatchTimeout=window.setTimeout(()=>{n.dispatchEvent(new Event("change",{bubbles:!0}))},200)}inputKeydownHandler(t){t.target instanceof HTMLInputElement&&o.isType(t.target,"integer")&&(t.key==="ArrowUp"?(t.preventDefault(),t.target.value===""&&(t.target.value="0"),t.target.value=(parseInt(t.target.value)+1).toString()):t.key==="ArrowDown"&&(parseInt(t.target.value)>0?t.target.value=(parseInt(t.target.value)-1).toString():t.target.value="0"))}destroy(){this.removeEventListeners(),this.originalNoValidate||this.form.removeAttribute("novalidate")}}return x});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jdlien/validator",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "dist/validator.js",
|
|
6
6
|
"types": "dist/Validator.d.ts",
|
|
@@ -59,6 +59,6 @@
|
|
|
59
59
|
},
|
|
60
60
|
"sideEffects": false,
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@jdlien/validator-utils": "^1.2.
|
|
62
|
+
"@jdlien/validator-utils": "^1.2.7"
|
|
63
63
|
}
|
|
64
64
|
}
|