kilvalidate 1.0.61
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 +248 -0
- package/kilvish.min.js +1 -0
- package/package.json +18 -0
package/README.md
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
KilValidate Cdn
|
|
2
|
+
|
|
3
|
+
This JavaScript package provides input field validation for web forms. It includes methods for validating various types of form fields, such as text fields, email, phone numbers, and custom patterns.
|
|
4
|
+
|
|
5
|
+
Installation
|
|
6
|
+
Kilvish Validation Attributes
|
|
7
|
+
|
|
8
|
+
This README describes the custom validation attributes (data-kilvish-*) used in your project for various input field types. These attributes are used to enforce constraints on the data entered into form fields.
|
|
9
|
+
|
|
10
|
+
Overview: These data-kilvish-* attributes are designed to ensure that the input provided in form fields adheres to specific validation rules, such as numbers only, alphanumeric characters, or specific special characters. Below is a breakdown of the available attributes.
|
|
11
|
+
|
|
12
|
+
Validation Attributes:
|
|
13
|
+
|
|
14
|
+
For Numbers Only
|
|
15
|
+
Attribute: data-kilvish-num='yes'
|
|
16
|
+
|
|
17
|
+
This attribute ensures that the input is a numeric value only.
|
|
18
|
+
|
|
19
|
+
Example:
|
|
20
|
+
|
|
21
|
+
For Numbers Only with Limits (Character Length)
|
|
22
|
+
Attribute: data-kilvish-num='_7_20'
|
|
23
|
+
|
|
24
|
+
This attribute restricts the input to numeric values only, and the length of the number must be between 7 and 20 characters.
|
|
25
|
+
|
|
26
|
+
Example:
|
|
27
|
+
|
|
28
|
+
For Alphanumeric Characters Only
|
|
29
|
+
Attribute: data-kilvish-char='mix'
|
|
30
|
+
|
|
31
|
+
This attribute allows only alphanumeric characters (letters and numbers).
|
|
32
|
+
|
|
33
|
+
Example:
|
|
34
|
+
|
|
35
|
+
For Alphanumeric Characters with Special Characters
|
|
36
|
+
Attribute: data-kilvish-char='mix_%_$'
|
|
37
|
+
|
|
38
|
+
This attribute allows alphanumeric characters along with specific special characters (%, _, and $).
|
|
39
|
+
|
|
40
|
+
Example:
|
|
41
|
+
|
|
42
|
+
or data-kilvish-char='mix_ ,-_' // ideal for address contain space , dash, comma
|
|
43
|
+
|
|
44
|
+
For Alphabets Only (Letters Only)
|
|
45
|
+
Attribute: data-kilvish-char='Yes'
|
|
46
|
+
|
|
47
|
+
This attribute restricts the input to alphabets (letters only, no numbers or special characters).
|
|
48
|
+
|
|
49
|
+
Example:
|
|
50
|
+
|
|
51
|
+
For Alphabets Only with Special Characters Allowed
|
|
52
|
+
Attribute: data-kilvish-char='Yes_%_$'
|
|
53
|
+
|
|
54
|
+
This attribute allows alphabets and special characters (%, _, and $).
|
|
55
|
+
|
|
56
|
+
Example:
|
|
57
|
+
|
|
58
|
+
File Input Validation with data-kilvish-file
|
|
59
|
+
7.1 -> use data-kilvish-file="image" or no use for simple Image
|
|
60
|
+
|
|
61
|
+
7.2 data-kilvish-file="image_min_max"
|
|
62
|
+
|
|
63
|
+
Example: data-kilvish-file="image_1_3" restricts the selection to between 1 and 3 image files.
|
|
64
|
+
7.3 data-kilvish-file="doc" for pdf , word , excel
|
|
65
|
+
|
|
66
|
+
7.4 data-kilvish-file="doc_min_max" Example: data-kilvish-file="doc_1_4" restricts the selection to between 1 and 4 document files.
|
|
67
|
+
|
|
68
|
+
7.5 data-kilvish-file="pdf" only allow pdf
|
|
69
|
+
|
|
70
|
+
7.6 data-kilvish-file="pdf_min_max"
|
|
71
|
+
|
|
72
|
+
Example: data-kilvish-file="pdf_1_4" restricts the selection to between 1 and 4 PDF files.
|
|
73
|
+
7.7 data-kilvish-file="mix" // for allow both image or doc
|
|
74
|
+
|
|
75
|
+
7.8 data-kilvish-file="only_pdf_image" → Only PDF and image files will be accepted.
|
|
76
|
+
|
|
77
|
+
7.9 data-kilvish-file="only_doc_image" → Only DOC-related and image files will be accepted.
|
|
78
|
+
|
|
79
|
+
Example: // limits to 1–4 files
|
|
80
|
+
|
|
81
|
+
data-kilvish-password="8_20" for min and max pass
|
|
82
|
+
Usage: To use these validation attributes, simply add the appropriate data-kilvish-* attribute to the tag in your HTML form. The validation will be applied automatically based on the specified rules.
|
|
83
|
+
|
|
84
|
+
Example:
|
|
85
|
+
|
|
86
|
+
Phone Number (7-20 digits):
|
|
87
|
+
Username (letters and numbers):
|
|
88
|
+
|
|
89
|
+
Password (letters, numbers, and special chars):
|
|
90
|
+
|
|
91
|
+
data-kilvish-amount // for setting amount or Price related validation
|
|
92
|
+
Case 1: data-kilvish-amount="USD" Input: 2 → Auto-formats to 2.00 with cursor at 2|.00. Input: 25. → Allows further typing for decimal values, e.g., 25.5 or 25.55. Input: 25.555 → Automatically truncates to 25.55.
|
|
93
|
+
|
|
94
|
+
Case 2: data-kilvish-amount="USD_200_1000" Input: 150 → Shows error: Amount must be at least 200.00 USD. Input: 250.333 → Automatically truncates to 250.33. Input: 1100 → Shows error: Amount must not exceed 1000.00 USD.
|
|
95
|
+
|
|
96
|
+
data-kilvish-date // Date Validations with # Date Validations with data-kilvish-date
|
|
97
|
+
Example: <input type="date" data-kilvish-date="_18_60">
|
|
98
|
+
Ensures the user is between 18 and 60 years old.
|
|
99
|
+
|
|
100
|
+
10.1 Future Date Restrictions data-kilvish-date="future"
|
|
101
|
+
Ensures the date must be in the future.
|
|
102
|
+
|
|
103
|
+
Example: <input type="date" data-kilvish-date="future">
|
|
104
|
+
|
|
105
|
+
The selected date must be after today.
|
|
106
|
+
Example: <input type="date" data-kilvish-date="today_future">
|
|
107
|
+
|
|
108
|
+
The selected date can be today or in the future.
|
|
109
|
+
Example: <input type="date" data-kilvish-date="30_days_future">
|
|
110
|
+
|
|
111
|
+
The selected date must be within the next 30 days.
|
|
112
|
+
|
|
113
|
+
Example: <input type="date" data-kilvish-date="2_years_future">
|
|
114
|
+
|
|
115
|
+
The date must be within the next 2 years.
|
|
116
|
+
|
|
117
|
+
Example: <input type="date" data-kilvish-date="2_years_after">
|
|
118
|
+
|
|
119
|
+
The date must be after 2 years from today
|
|
120
|
+
|
|
121
|
+
10.2 Past Date Restrictions data-kilvish-date="2_years_past"
|
|
122
|
+
Ensures the date must be in the past.
|
|
123
|
+
|
|
124
|
+
Usage: data-kilvish-date="past"
|
|
125
|
+
|
|
126
|
+
Example: <input type="date" data-kilvish-date="past">
|
|
127
|
+
|
|
128
|
+
The date must be within the past 2 years.
|
|
129
|
+
Example: <input type="date" data-kilvish-date="10_years_past">
|
|
130
|
+
|
|
131
|
+
The date must be within the past 10 years.
|
|
132
|
+
|
|
133
|
+
Example: <input type="date" data-kilvish-date="2_years_before">
|
|
134
|
+
|
|
135
|
+
The date must be before 2 years ago.
|
|
136
|
+
10.3 Date Range (Custom) data-kilvish-date="Between_2024_2025"
|
|
137
|
+
Ensures the date falls within a specific range of years.
|
|
138
|
+
|
|
139
|
+
Usage: data-kilvish-date="Between_startYear_endYear"
|
|
140
|
+
Example: <input type="date" data-kilvish-date="Between_2000_2010">
|
|
141
|
+
Ensures the date is between 2000 and 2010.
|
|
142
|
+
Example: <input type="date" data-kilvish-date="Between_1920_1985">
|
|
143
|
+
Ensures the date is between 1920 and 1985.
|
|
144
|
+
10.4 Relative Date Validation data-kilvish-date="from_2023" OR "till_2025"
|
|
145
|
+
Allows validation based on specific starting or ending years.
|
|
146
|
+
|
|
147
|
+
Usage: data-kilvish-date="from_year" or data-kilvish-date="till_year"
|
|
148
|
+
Example: <input type="date" data-kilvish-date="from_2023">
|
|
149
|
+
The date must be from 2023 onwards.
|
|
150
|
+
Example: <input type="date" data-kilvish-date="till_2025">
|
|
151
|
+
The date must be till 2025 (including 2025 but not beyond).
|
|
152
|
+
10.5 Date Should Not Be Today - data-kilvish-date="not_today"
|
|
153
|
+
Prevents users from selecting today's date.
|
|
154
|
+
|
|
155
|
+
Usage: data-kilvish-date="not_today"
|
|
156
|
+
Example: <input type="date" data-kilvish-date="not_today">
|
|
157
|
+
Today's date is not allowed.
|
|
158
|
+
10.6 Weekday or Weekend Restrictions data-kilvish-date="weekend" OR "weekday"
|
|
159
|
+
Restricts the date to either a weekday or a weekend.
|
|
160
|
+
|
|
161
|
+
Usage: data-kilvish-date="weekday" or data-kilvish-date="weekend"
|
|
162
|
+
Example: <input type="date" data-kilvish-date="weekday">
|
|
163
|
+
The selected date must be a Monday to Friday.
|
|
164
|
+
Example: <input type="date" data-kilvish-date="weekend">
|
|
165
|
+
The selected date must be a Saturday or Sunday.
|
|
166
|
+
10.7 Exact Age Validation data-kilvish-date="exact_21"
|
|
167
|
+
Ensures the user is a specific age.
|
|
168
|
+
|
|
169
|
+
Usage: data-kilvish-date="exact_age"
|
|
170
|
+
Example: <input type="date" data-kilvish-date="exact_21">
|
|
171
|
+
The user must be exactly 21 years old.
|
|
172
|
+
10.8 Specific Days Validation data-kilvish-date="first_of_month" OR "last_of_month"
|
|
173
|
+
Restricts the date to the first or last day of the month.
|
|
174
|
+
|
|
175
|
+
Usage: data-kilvish-date="first_of_month" or data-kilvish-date="last_of_month"
|
|
176
|
+
Example: <input type="date" data-kilvish-date="first_of_month">
|
|
177
|
+
The selected date must be the first day of any month.
|
|
178
|
+
Example: <input type="date" data-kilvish-date="last_of_month">
|
|
179
|
+
The selected date must be the last day of any month.
|
|
180
|
+
10.9 Leap Year Validation ( NOt Developed Yet - Do NOt Use )
|
|
181
|
+
Ensures the date falls in a leap year.
|
|
182
|
+
|
|
183
|
+
Usage: data-kilvish-date="leap_year"
|
|
184
|
+
Example: <input type="date" data-kilvish-date="leap_year">
|
|
185
|
+
The selected date must be in a leap year.
|
|
186
|
+
10.10 Combining Multiple Conditions ( NOt Developed Yet - Do NOt Use )
|
|
187
|
+
You can combine multiple conditions using _.
|
|
188
|
+
|
|
189
|
+
Example: <input type="date" data-kilvish-date="future_weekday">
|
|
190
|
+
The selected date must be a future date and a weekday.
|
|
191
|
+
Example: <input type="date" data-kilvish-date="past_30_days">
|
|
192
|
+
The selected date must be in the past, but within the last 30 days.
|
|
193
|
+
10.10 IF Age should be not pass perticular years in the Past or Future ( Old technique)
|
|
194
|
+
user in input name='kildate' and put data-kilvish-date='_18_100' and type='date' You can combine multiple conditions using _.
|
|
195
|
+
|
|
196
|
+
Example: <input type="date" data-kilvish-date="_18_100">
|
|
197
|
+
Birthday cannot be less than 18 years ago or more than 100 years in the future.**.
|
|
198
|
+
10.11 Date Group Validation — Start and End Dates (data-kilvish-dategroup)
|
|
199
|
+
This attribute links two related date or month inputs (such as employment start and end dates) to enforce a logical sequence — for example, the "From" date should not be after the "To" date.
|
|
200
|
+
|
|
201
|
+
Format: data-kilvish-dategroup="X_Y" Y will be 1 or 2 ...so on
|
|
202
|
+
|
|
203
|
+
data-kilvish-date_name is optional if you want validaion of (fromDate to Todate )
|
|
204
|
+
|
|
205
|
+
data-kilvish-dategroup="GROUP_1" & data-kilvish-date_name
|
|
206
|
+
data-kilvish-dategroup="GROUP_2" & data-kilvish-date_name
|
|
207
|
+
|
|
208
|
+
X is any unique group identifier (e.g., A, EMP, SCHOOL)
|
|
209
|
+
|
|
210
|
+
Y is 1 or 2
|
|
211
|
+
|
|
212
|
+
1 should be used for the earlier date field (e.g., start date)
|
|
213
|
+
|
|
214
|
+
2 should be used for the later date field (e.g., end date)
|
|
215
|
+
|
|
216
|
+
Or with data-kilvish-date_name
|
|
217
|
+
|
|
218
|
+
Use data-kilvish-date_name to show user-friendly error messages instead of raw identifiers like EMP_1.
|
|
219
|
+
|
|
220
|
+
Age Validation Using data-kilvish-age
|
|
221
|
+
Use this attribute to validate age from a plain or field.
|
|
222
|
+
|
|
223
|
+
Attribute: data-kilvish-age="value" or data-kilvish-age="min_max"
|
|
224
|
+
|
|
225
|
+
Usage Patterns: data-kilvish-age="18" → Requires the user to be exactly 18 years old.
|
|
226
|
+
|
|
227
|
+
data-kilvish-age="18_60" → Accepts only ages between 18 and 60 (inclusive).
|
|
228
|
+
|
|
229
|
+
If the attribute is omitted, it defaults to exactly 18.
|
|
230
|
+
|
|
231
|
+
Validation Behavior: Input must be a valid number.
|
|
232
|
+
|
|
233
|
+
Field is cleared and error message shown if:
|
|
234
|
+
|
|
235
|
+
Non-numeric value is entered.
|
|
236
|
+
|
|
237
|
+
Age is below or above the defined limits.
|
|
238
|
+
|
|
239
|
+
Notes:
|
|
240
|
+
|
|
241
|
+
The attributes should be added to input elements where validation is required.
|
|
242
|
+
These validation rules are handled client-side, so users will be notified immediately if they input invalid data.
|
|
243
|
+
Customize the validation behavior further as needed (e.g., setting up custom error messages).
|
|
244
|
+
Author: Vasu Birla kilvishbirla@gmail.com
|
|
245
|
+
|
|
246
|
+
You can include this script in your project via CDN:
|
|
247
|
+
|
|
248
|
+
<script src="https://cdn.jsdelivr.net/npm/kilcdn@latest/kilvish.js"></script>
|
package/kilvish.min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function validateRequiredFields(e){const t=document.querySelector(`${e}`),r=t.querySelectorAll("input[required], select[required], textarea[required]"),a=(t.querySelectorAll("fieldset.radios"),t.querySelectorAll('input[type="radio"]')),s={},o=new Set;t.querySelectorAll('input[type="checkbox"]').forEach((e=>{const t=e.name;s[t]||(s[t]=[]),s[t].push(e),e.hasAttribute("required")&&o.add(t)}));t.querySelectorAll(".select2");let l=!0,d="This Field is Required.";if(t.querySelectorAll(".error-message").forEach((e=>e.remove())),r.forEach((e=>{e.style.border=""})),r.forEach((e=>{const t=e.closest(".form-group")||e.parentNode;e.parentNode;if("date"==e.type){d="Please Select Date",e.style.border="";e.value.trim()||(l=!1,e.style.border="1px solid red",addErrorMessage(t,d))}else d="comments"==e.name?"Please Enter Comments":"email"==e.type?"Please Enter Email":"password"==e.type?"Please Enter Password":"This Field is Required.";e.value.trim()||(l=!1,e.style.border="1px solid red",addErrorMessage(t,d))})),a.length>0){const e={};a.forEach((t=>{const r=t.name;e[r]||(e[r]={isSelected:!1,groupElement:t.closest(".form-group")||t.closest(".mb-3")||t.closest(".form-check")||t.closest(".radios")||t.closest(".col-md-6")}),t.checked&&(e[r].isSelected=!0)})),Object.entries(e).forEach((([e,t])=>{if(!t.isSelected){l=!1;const r="Please select one option.";if(!t.groupElement)return void console.error("Invalid group element for radio group:",e);addErrorMessage(t.groupElement,r)}}))}return Object.keys(s).forEach((e=>{const t=s[e];if(t.some((e=>e.hasAttribute("data-ignore-kilvish"))))return;if(!t.some((e=>e.checked))){l=!1;const r=o.has(e)?"Please check this box to proceed":"Please select at least one option.";addErrorMessage(t[0].closest(".form-group")||t[0].closest(".form-check")||t[0].closest(".checkbox-container")||t[0].closest(".some-other-container"),r)}})),l}function addRealTimeValidation(e){const t=document.getElementById(e),r=t.querySelectorAll("input[required], select[required], textarea[required]"),a=(e,r=!0)=>{let a=t.querySelector(`label[for='${e.name}']`);if(!a){const t=e.closest("fieldset");t&&(a=t.querySelector("legend"))}a&&(r&&!a.innerHTML.includes("*")?a.innerHTML+=' <span style="color:red;">*</span>':!r&&a.innerHTML.includes("*")&&(a.innerHTML=a.innerHTML.replace(' <span style="color:red;">*</span>',"")))};r.forEach((e=>{const t=()=>{e.value.trim()?((e=>{const t=(e.closest(".form-group")||e.closest(".form-check")||e.closest(".radios")||e.closest(".checkbox-container")||e.parentNode).querySelector(".error-message");t&&t.remove(),e.style.border=""})(e),a(e,!1)):a(e,!0)};e.addEventListener("input",t),e.addEventListener("change",t)}));const s=t.querySelectorAll('input[type="radio"]');[...new Set(Array.from(s).map((e=>e.name)))].forEach((e=>{const r=t.querySelectorAll(`input[type="radio"][name="${e}"]`);r.forEach((e=>{e.addEventListener("change",(()=>{if(Array.from(r).some((e=>e.checked))){const t=e.closest(".form-group")||e.closest(".form-check")||e.closest(".radios")||e.closest(".mb-3")||e.closest(".col-md-6"),r=t?.querySelector(".error-message");r&&r.remove()}}))}))}));const o=t.querySelectorAll('input[type="checkbox"]');[...new Set(Array.from(o).map((e=>e.name)))].forEach((e=>{const r=t.querySelectorAll(`input[type="checkbox"][name="${e}"]`);r.forEach((e=>{e.addEventListener("change",(()=>{if(Array.from(r).some((e=>e.checked))){const t=e.closest(".form-group")||e.closest(".form-check")||e.closest(".checkbox-container")||e.closest(".some-other-container"),r=t?.querySelector(".error-message");r&&r.remove()}}))}))}))}function addRealTimeValidation_working(e){const t=document.getElementById(e),r=t.querySelectorAll("input[required], select[required], textarea[required]"),a=(e,r=!0)=>{let a=t.querySelector(`label[for='${e.name}']`);if(!a){const t=e.closest("fieldset");t&&(a=t.querySelector("legend"))}a&&(r&&!a.innerHTML.includes("*")?a.innerHTML+=' <span style="color:red;">*</span>':!r&&a.innerHTML.includes("*")&&(a.innerHTML=a.innerHTML.replace(' <span style="color:red;">*</span>',"")))};r.forEach((e=>{const t=()=>{e.value.trim()?((e=>{e.closest(".form-group")||e.parentNode;const t=(e.closest(".form-group")||e.closest(".form-check")||e.closest(".radios")||e.closest(".checkbox-container")||e.parentNode).querySelector(".error-message");t&&t.remove(),e.style.border=""})(e),a(e,!1)):a(e,!0)};e.addEventListener("input",t),e.addEventListener("change",t)}))}function validateForm(e){validateRequiredFields(e)||event.preventDefault()}function addAsteriskToRequiredFields(){document.querySelectorAll("input, select, textarea").forEach((e=>{if(e.required){let t;if(t=document.querySelector(`label[for='${e.name}']`),!t){const r=e.closest("fieldset");r&&(t=r.querySelector("legend"))}t&&!t.innerHTML.includes("*")&&(t.innerHTML+=' <span style="color:red;">*</span>')}}))}function attachInputEvents(){document.querySelectorAll("input, select, textarea").forEach((e=>{e.hasAttribute("data-ignore-kilvish")||(e.addEventListener("keyup",validateKilvishInput),e.addEventListener("change",validateKilvishInput))}))}function addValidateFormToSubmitButtons(){document.querySelectorAll("form").forEach((e=>{const t=e.id;if(t){e.querySelectorAll('button[type="submit"], input[type="submit"]').forEach((e=>{e.setAttribute("onclick",`validateForm('#${t}')`)}))}else console.warn("Form without ID found. Skipping validation.")}))}function validateKilvishInput(e){const t=e.target;let r=/^[a-zA-Z\s]*$/;const a={image:["image/jpeg","image/png","image/bmp","image/tiff","image/webp","image/svg+xml"],doc:["application/pdf","application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],pdf:["application/pdf"],mix:["image/jpeg","image/png","image/bmp","image/tiff","image/webp","image/svg+xml","application/pdf","application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],only_pdf_image:["application/pdf","image/jpeg","image/png","image/bmp","image/tiff","image/webp","image/svg+xml"],only_doc_image:["application/pdf","application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","image/jpeg","image/png","image/bmp","image/tiff","image/webp","image/svg+xml"]},s={image:"Image",doc:"Document",pdf:"PDF",mix:"Image & Document",only_pdf_image:"PDF & Image",only_doc_image:"Document & Image"},o=t.closest(".form-group")||t.closest(".col-lg-12")||t.closest("div")||t.parentNode;t.parentNode;let l=o.querySelector(".error-message");if(l&&l.remove(),t.style.border="","description"===t.name||"address"===t.name){if(r=/^[a-zA-Z0-9@,._\s-'"*]*$/,t.value.length>400)return addErrorMessage(o,"Input exceeds the maximum length of 400 characters!"),t.style.border="1px solid red",void(t.value=t.value.substring(0,400))}else if("firstname"===t.name||"lastname"===t.name||"appointment_by"===t.name){r=/^[a-zA-Z\s-]*$/;const a=/^[a-zA-Z0-9._@-\s]+$/,s=t.value;if(a.test(t.value)){if(s.length>20||s.length<2)return addErrorMessage(o,"This Field Must Be Between 2 And 20 Characters."),t.style.border="1px solid red",void("change"===e.type&&(t.value=""))}else;t.style.border=""}else if("otp"===t.name){r=/^\d{6,6}$/;const a=/^\d+$/,s=t.value;parseInt(s,10);if(a.test(t.value)){if(s.length>6||s.length<6)return addErrorMessage(o,"OTP must be exactly 6 digits"),t.style.border="1px solid red",void("change"===e.type&&(t.value=""))}else;t.style.border=""}else if("number_of_items"===t.name){r=/^\d{0,6}(\.\d{0,2})?$/;const a=/^\d+$/,s=t.value;parseInt(s,10);if(a.test(t.value)){if(s.length>3)return addErrorMessage(o,"Pacakge Number Cannot Be Exceed 3 Digits"),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else;t.style.border=""}else if("contact"===t.name||"agent_contact"==t.id)r=/^[\d\-\s]{0,20}$/;else if("number"==t.type){r=/^\d{0,20}$/;const a=/^\d+$/,s=t.value;parseInt(s,10);if(a.test(t.value)){if(s.length>20||s.length<1)return addErrorMessage(o,"This Must Be Between 1 And 20 Digits."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else;t.style.border=""}else if("tel"===t.type){let a=t.value.trim();a=a.replace(/\s+/g,""),t.value=a;if(r=/^\+?\d{7,15}$/,!/^\+?\d{7,15}$/.test(a))return addErrorMessage(o,"Enter a valid phone number (7–15 digits, optional +)."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));t.style.border=""}else if("kildate"===t.name){r=/^(?:\d{4}[-\/]\d{2}[-\/]\d{2})$/;const a=t.value.trim(),s=new Date(a.replace(/[-\/]/g,"/")),l=new Date;l.setHours(0,0,0,0);const d=t.getAttribute("data-kilvish-date");if(d){const[r,a]=d.replace("_","").split("_").map(Number),n=new Date(l),i=new Date(l);if(n.setFullYear(l.getFullYear()-r),i.setFullYear(l.getFullYear()-a),s>n||s<i)return addErrorMessage(o,`Birthday cannot be less than ${r} years ago or more than ${a} years in the future.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(s<l)return addErrorMessage(o,"The date cannot be in the past."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));t.style.border=""}else{if("date"===t.type||"month"===t.type){r=/^(?:\d{4}[-\/]\d{2}[-\/]\d{2})$/;const a=t.value.trim(),s="month"===t.type?new Date(a+"-01"):new Date(a.replace(/[-\/]/g,"/")),l=new Date;l.setHours(0,0,0,0);const d=t.getAttribute("data-kilvish-date");if(d)if(/^_(\d+)_(\d+)$/.test(d)){const[r,a]=d.replace("_","").split("_").map(Number),n=new Date(l),i=new Date(l);if(n.setFullYear(l.getFullYear()-r),i.setFullYear(l.getFullYear()-a),s>n||s<i)return addErrorMessage(o,`Age must be between ${r} and ${a} years.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/^_(\d+)$/.test(d)){const r=Number(d.replace("_","")),a=new Date(l);if(a.setFullYear(l.getFullYear()-r),s.getTime()!==a.getTime())return addErrorMessage(o,`You must be exactly ${r} years old.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/future/.test(d)){if("future"===d&&s<=l)return addErrorMessage(o,"The selected date must be in the future."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));if("today_future"===d&&s<l)return addErrorMessage(o,"The selected date cannot be in the past."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));if(/(\d+)_days_future/.test(d)){const r=Number(d.match(/(\d+)_days_future/)[1]),a=new Date(l);if(a.setDate(l.getDate()+r),s>a||s<l)return addErrorMessage(o,`The selected date must be within the next ${r} days.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/^(\d+)_years_future$/.test(d)){const r=Number(d.match(/^(\d+)_years_future$/)[1]),a=new Date(l);if(a.setFullYear(l.getFullYear()+r),s<l||s>a)return addErrorMessage(o,`The date must be within the next ${r} years.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/^(\d+)_years_after$/.test(d)){const r=Number(d.match(/^(\d+)_years_after$/)[1]),a=new Date(l);if(a.setFullYear(l.getFullYear()+r),s.getTime()!==a.getTime())return addErrorMessage(o,`The date must be exactly ${r} years after today.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}}else if(/past/.test(d)){if("past"===d&&s>=l)return addErrorMessage(o,"The selected date must be in the past."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));if(/^(\d+)_years_past$/.test(d)){const r=Number(d.match(/^(\d+)_years_past$/)[1]),a=new Date(l);if(a.setFullYear(l.getFullYear()-r),s>l||s<a)return addErrorMessage(o,`The date must be within the past ${r} years.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}}else if(/^(\d+)_years_before$/.test(d)){const r=Number(d.match(/^(\d+)_years_before$/)[1]),a=new Date(l);if(a.setFullYear(l.getFullYear()-r),s>=a)return addErrorMessage(o,`The date must be before ${r} years ago.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/^(\d+)_years_after$/.test(d)){const r=Number(d.match(/^(\d+)_years_after$/)[1]),a=new Date(l);if(a.setFullYear(l.getFullYear()+r),s<=a)return addErrorMessage(o,`The date must be after ${r} years from today.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/Between_(\d+)_(\d+)/.test(d)){const[r,a]=d.match(/Between_(\d+)_(\d+)/).slice(1,3).map(Number),l=new Date(r,0,1),n=new Date(a,11,31);if(s<l||s>n)return addErrorMessage(o,`The date must be between ${r} and ${a}.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/from_(\d+)/.test(d)){const r=Number(d.match(/from_(\d+)/)[1]);if(s<new Date(r,0,1))return addErrorMessage(o,`The date must be from the year ${r} onwards.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/till_(\d+)/.test(d)){const r=Number(d.match(/till_(\d+)/)[1]);if(s>new Date(r,11,31))return addErrorMessage(o,`The date must be till the year ${r}.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else{if("not_today"===d&&s.getTime()===l.getTime())return addErrorMessage(o,"Today's date is not allowed."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));if("weekday"===d&&(0===s.getDay()||6===s.getDay()))return addErrorMessage(o,"Only weekdays (Monday to Friday) are allowed."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));if("weekend"===d&&s.getDay()>=1&&s.getDay()<=5)return addErrorMessage(o,"Only weekends (Saturday and Sunday) are allowed."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));if("first_of_month"===d){if(1!==s.getDate())return addErrorMessage(o,"The date must be the first day of the month."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/^exact_(\d+)$/.test(d)){const r=Number(d.match(/^exact_(\d+)$/)[1]),a=new Date(l);if(a.setFullYear(l.getFullYear()-r),s.getTime()!==a.getTime())return addErrorMessage(o,`You must be exactly ${r} years old.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if("last_of_month"===d){const r=new Date(s.getFullYear(),s.getMonth()+1,0).getDate();if(s.getDate()!==r)return addErrorMessage(o,"The date must be the last day of the month."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}}const n=t.getAttribute("data-kilvish-dategroup");if(n&&/.+_[12]$/.test(n)){const[r,a]=n.split("_"),s="1"===a?"2":"1",l=`[data-kilvish-dategroup="${r}_${s}"]`,d=document.querySelector(l);if(d&&d.value){const l="month"===t.type?new Date(t.value+"-01"):new Date(t.value.replace(/[-\/]/g,"/")),n="month"===d.type?new Date(d.value+"-01"):new Date(d.value.replace(/[-\/]/g,"/"));if(!("1"===a&&l<=n||"2"===a&&l>=n)){return addErrorMessage(o,`${t.dataset.kilvishDate_name||`Field (${r}_${a})`} must be ${"1"===a?"before or same as":"after or same as"} ${d.dataset.kilvishDate_name||`Field (${r}_${s})`}.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}}}return void(t.style.border="")}if("age"===t.name){const r=t.value.trim(),a=parseInt(r,10);if(isNaN(a))return addErrorMessage(o,"Please enter a valid number for age."),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));const s=t.getAttribute("data-kilvish-age")||"18";if(/^(\d+)$/.test(s)){const r=Number(s);if(a!==r)return addErrorMessage(o,`Age must be exactly ${r} years.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}else if(/^(\d+)_(\d+)$/.test(s)){const[r,l]=s.split("_").map(Number);if(a<r)return addErrorMessage(o,`Minimum age must be ${r} years.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""));if(a>l)return addErrorMessage(o,`Age cannot exceed ${l} years.`),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}return void(t.style.border="")}if("model_name"===t.name||"models[]"===t.name)r=/^[a-zA-Z0-9]{4,20}$/;else if("email"==t.type){r=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;const a=/^[a-zA-Z0-9._@-]+$/,s=t.value;if(a.test(s)){if(!r.test(s))return addErrorMessage(o,"Invalid Email Format."),t.style.border="1px solid red",void("change"===e.type&&(t.value=""))}else;t.style.border=""}else if(t.hasAttribute("data-kilvish-num")){const a=t.value,s=t.getAttribute("data-kilvish-num");r=/^[0-9]*$/;let l=null,d=null;const n=/^_(\d+)_(\d+)$/,i=s.match(n);i&&(l=parseInt(i[1],10),d=parseInt(i[2],10));const c=/^exact_(\d+)$/,u=s.match(c);let p=null;u&&(p=parseInt(u[1],10)),/^\d*$/.test(a)?null!==p&&a.length!==p?(addErrorMessage(o,`This field must contain exactly ${p} digits.`),t.style.border="2px solid red","change"===e.type&&(t.value="")):l&&a.length<l||d&&a.length>d?(addErrorMessage(o,`This Field Must be between ${l} and ${d} digits.`),t.style.border="2px solid red","change"===e.type&&(t.value="")):t.style.border="":(addErrorMessage(o,"Only numerical characters are allowed."),t.style.border="2px solid red")}else if(t.hasAttribute("data-kilvish-num11")){const a=t.value,s=t.getAttribute("data-kilvish-num");r=/^[0-9]*$/;let l=null,d=null;const n=/^_(\d+)_(\d+)$/,i=s.match(n);i&&(l=parseInt(i[1],10),d=parseInt(i[2],10)),"exact_20"===s&&(r=/^[0-9]{20}$/),r.test(a)?l&&a.length<l||d&&a.length>d?(addErrorMessage(o,`This Field Must be between ${l} and ${d} digits.`),t.style.border="2px solid red","change"===e.type&&(t.value="")):t.style.border="":(addErrorMessage(o,"exact_20"===s?"This field must contain exactly 20 digits.":"Only numerical characters are allowed."),t.style.border="2px solid red")}else if(t.hasAttribute("data-kilvish-char")){const a=t.value,s=t.getAttribute("data-kilvish-char");if(s.startsWith("Yes")||s.startsWith("mix")){"mix"===s?r=/^[a-zA-Z0-9]*$/:"Yes"===s&&(r=/^[a-zA-Z]*$/);const l=s.split("_").slice(1).join("");if(l&&(r=new RegExp(`^[a-zA-Z0-9${l}]*$`)),!r.test(a))return addErrorMessage(o,"Invalid character entered!"),t.style.border="2px solid red",void("change"===e.type&&(t.value=""))}}else if(t.hasAttribute("data-kilvish-amount")){const a=t.getAttribute("data-kilvish-amount");r=/^\d+(\.\d{0,2})?$/;let s="USD",o=null,l=null;const d=/^([A-Z]{3})(?:_(\d+)_(\d+))?$/,n=a.match(d);n&&(s=n[1],o=n[2]?parseFloat(n[2]):null,l=n[3]?parseFloat(n[3]):null),t.addEventListener("input",(e=>{let a=t.selectionStart,o=t.value;r.test(o)||(o=o.replace(/[^0-9.]/g,""),o=o.replace(/(\..*?)\..*/,"$1"),o=o.replace(/(\.\d{2}).*/,"$1")),!o||o.includes(".")||o.endsWith(".")||(o=parseFloat(o).toFixed(2)),t.value=o;const l=o.indexOf(".");-1!==l&&a>l&&(a=Math.min(a,o.length)),t.setSelectionRange(a,a);let d=t.nextElementSibling;d&&d.classList.contains("amount-display")||(d=document.createElement("div"),d.className="amount-display",d.style.color="green",t.parentNode.insertBefore(d,t.nextSibling)),d.textContent=`${s} ${o||"0.00"} .`})),t.addEventListener("blur",(()=>{if(t.readOnly)return;let r=parseFloat(t.value||"0").toFixed(2),a=t.nextElementSibling;return null!==o&&r<o?(addErrorMessage(t.parentNode,`Amount must be at least ${o.toFixed(2)} ${s}.`),t.style.border="2px solid red",void("change"===e.type&&(t.value="",a.textContent=""))):null!==l&&r>l?(addErrorMessage(t.parentNode,`Amount must not exceed ${l.toFixed(2)} ${s}.`),t.style.border="2px solid red",void("change"===e.type&&(t.value="",a.textContent=""))):(t.style.border="",void(t.value=r))}))}else if("password_working"===t.type){const a=e=>{console.log("passworddddddd -",e);const t=e.length;for(let r=0;r<t-3;r++)if(e.charCodeAt(r+1)===e.charCodeAt(r)+1&&e.charCodeAt(r+2)===e.charCodeAt(r)+2&&e.charCodeAt(r+3)===e.charCodeAt(r)+3)return!0;for(let r=0;r<t-3;r++)if(e.charCodeAt(r+1)===e.charCodeAt(r)-1&&e.charCodeAt(r+2)===e.charCodeAt(r)-2&&e.charCodeAt(r+3)===e.charCodeAt(r)-3)return!0;return!1},s=t.getAttribute("data-kilvish-password")||"12",[l,d]=s.split("_").map(Number),n=l||12,i=d||1/0;if(r=new RegExp(`^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[\\W_]).{${n},${i}}$`),!r.test(t.value))return addErrorMessage(t.parentElement,`Password must be between ${n} and ${i} characters long and contain at least one uppercase letter, one lowercase letter, one special character, and one number.`),t.style.border="1px solid red",void("change"===e.type&&(t.value=""));if(a(t.value))return addErrorMessage(o,"Password should not contain sequential characters."),t.style.border="1px solid red",void("change"===e.type&&(t.value=""))}else if("password"===t.type){let a=t.parentElement.querySelector(".password-validation");a||(a=document.createElement("ul"),a.classList.add("password-validation"),a.style.listStyleType="none",a.style.padding="0",a.style.marginTop="5px",t.insertAdjacentElement("afterend",a));const s=t.getAttribute("data-kilvish-password")||"12",[o,l]=s.split("_").map(Number);r=new RegExp(`^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[\\W_]).{${o},${l}}$`);const d={length:`Password must be between ${o} and ${l} characters long`,uppercase:"At least one uppercase letter (A-Z)",lowercase:"At least one lowercase letter (a-z)",number:"At least one number (0-9)",special:"At least one special character (!@#$%^&*)",sequential:"No sequential characters (e.g., 1234, abcd)"},n={};Object.keys(d).forEach((e=>{let t=a.querySelector(`li[data-rule="${e}"]`);t||(t=document.createElement("li"),t.setAttribute("data-rule",e),t.innerHTML=`❌ ${d[e]}`,t.style.color="red",t.style.fontSize="14px",a.appendChild(t)),n[e]=t}));const i=e=>{for(let t=0;t<e.length-3;t++){if(e.charCodeAt(t+1)===e.charCodeAt(t)+1&&e.charCodeAt(t+2)===e.charCodeAt(t)+2&&e.charCodeAt(t+3)===e.charCodeAt(t)+3)return!0;if(e.charCodeAt(t+1)===e.charCodeAt(t)-1&&e.charCodeAt(t+2)===e.charCodeAt(t)-2&&e.charCodeAt(t+3)===e.charCodeAt(t)-3)return!0}return!1},c=t.value,u={length:c.length>=o&&c.length<=l,uppercase:/[A-Z]/.test(c),lowercase:/[a-z]/.test(c),number:/\d/.test(c),special:/[\W_]/.test(c),sequential:!i(c)};Object.keys(u).forEach((e=>{n[e].innerHTML=u[e]?`✅ ${d[e]}`:`❌ ${d[e]}`,n[e].style.color=u[e]?"green":"red",u[e]}));const p=r.test(c);t.style.border=p?"1px solid green":"1px solid red",p||"change"!==e.type||(t.value="")}}if("file"===t.type){const e=t.getAttribute("data-kilvish-file")||"image",[r,...l]=e.split("_");let d=a[e]||a[r]||a.image;const n=t.files;let i=1,c=1/0;if(l.length>=2&&(i=parseInt(l[0],10),c=parseInt(l[1],10)),n.length<i||n.length>c)return addErrorMessage(o,`Please select between ${i} and ${c} valid file(s).`),t.value="",t.style.border="1px solid red",!1;const u=s[e]||r.toUpperCase();for(const e of n)if(!d.includes(e.type))return addErrorMessage(o,`Invalid file type. Allowed types: ${u}.`),t.value="",t.style.border="1px solid red",!1}else"keyup"===e.type&&(r.test(t.value)||("password"!==t.type&&addErrorMessage(o,"Invalid character entered!"),t.style.border="1px solid red")),"change"===e.type&&(r.test(t.value)?(t.style.border="",l&&l.remove()):(t.value="",addErrorMessage(o,"Invalid character or format!"),t.style.border="1px solid red"))}function addErrorMessage(e,t){if(!e.querySelector(".error-message")){const r=document.createElement("div");r.className="error-message",r.style.color="red",r.style.fontSize="12px",r.innerText=t,e.appendChild(r)}}function addErrorMessage_working(e,t){if(!e.querySelector(".error-message")){const r=document.createElement("div");r.className="error-message",r.style.color="red",r.style.fontSize="12px",r.style.marginTop="4px",r.innerText=t;const a=e.querySelector(".input-group, input, select, textarea");a?a.insertAdjacentElement("afterend",r):e.appendChild(r)}}document.addEventListener("DOMContentLoaded",(()=>{document.querySelectorAll("form").forEach((e=>{const t=e.id;t?addRealTimeValidation(t):console.warn("Form without ID found. Skipping validation.")})),attachInputEvents(),addAsteriskToRequiredFields(),addValidateFormToSubmitButtons()})),document.addEventListener("DOMContentLoaded",(()=>{document.querySelectorAll("input[type='password']").forEach((e=>{if(e.hasAttribute("data-ignore-kilvish"))return;const t=document.createElement("div");t.style.display="flex",t.style.alignItems="center",t.style.marginTop="5px";const r=[];for(let e=0;e<3;e++){const e=document.createElement("div");e.style.height="5px",e.style.width="30px",e.style.margin="0 2px",e.style.borderRadius="3px",e.style.background="lightgray",r.push(e),t.appendChild(e)}const a=document.createElement("span");a.textContent="Weak",a.style.marginLeft="10px",a.style.fontSize="12px",a.style.color="gray",t.appendChild(a),e.parentNode.insertBefore(t,e.nextSibling);e.addEventListener("input",(e=>{const t=(e=>{let t=0;return e.length>=12&&(t+=2),/[A-Z]/.test(e)&&(t+=1),/[a-z]/.test(e)&&(t+=1),/\d/.test(e)&&(t+=1),/[\W_]/.test(e)&&(t+=1),(e=>{for(let t=0;t<e.length-3;t++){if(e.charCodeAt(t+1)===e.charCodeAt(t)+1&&e.charCodeAt(t+2)===e.charCodeAt(t)+2&&e.charCodeAt(t+3)===e.charCodeAt(t)+3)return!0;if(e.charCodeAt(t+1)===e.charCodeAt(t)-1&&e.charCodeAt(t+2)===e.charCodeAt(t)-2&&e.charCodeAt(t+3)===e.charCodeAt(t)-3)return!0}return!1})(e)&&(t-=2),Math.max(0,t)})(e.target.value);r.forEach((e=>e.style.background="lightgray")),t<=2?(r[0].style.background="red",a.textContent="Weak",a.style.color="red"):t<=4?(r[0].style.background="yellow",r[1].style.background="yellow",a.textContent="Medium",a.style.color="orange"):(r[0].style.background="green",r[1].style.background="green",r[2].style.background="green",a.textContent="Strong",a.style.color="green")}))}))}));
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kilvalidate",
|
|
3
|
+
"version": "1.0.61",
|
|
4
|
+
"main": "kilvish.min.js",
|
|
5
|
+
"files": [
|
|
6
|
+
"kilvish.min.js"
|
|
7
|
+
],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"description": "",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"terser": "^5.37.0"
|
|
17
|
+
}
|
|
18
|
+
}
|