derafu-js 0.1.0

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.
@@ -0,0 +1,482 @@
1
+ /*! Form validation functions | (c) 2025 Derafu DEV | MIT */
2
+
3
+ // Object where form validation functions will be assigned.
4
+ const FormValidation = {};
5
+
6
+ /**
7
+ * [DEPRECATED] Creates and displays a loading message in a modal dialog using
8
+ * bootbox. Useful for indicating to the user that an operation is in progress.
9
+ *
10
+ * @deprecated This function is deprecated and will be removed in future
11
+ * versions. Use UI.loading() instead.
12
+ * @param {string} message - Message to show to the user.
13
+ * @returns {*} Returns the result of UI.loading().
14
+ */
15
+ FormValidation.loading = function (message) {
16
+ 'use strict';
17
+ console.warn(
18
+ 'FormValidation.loading() is deprecated. Use UI.loading() instead.',
19
+ );
20
+ return UI.loading(message);
21
+ };
22
+
23
+ /**
24
+ * [DEPRECATED] Creates and displays an alert dialog using bootbox.
25
+ * Useful for showing warning or information messages to the user.
26
+ *
27
+ * @deprecated This function is deprecated and will be removed in future
28
+ * versions. Use UI.alert() instead.
29
+ * @param {string} message - Message to show to the user.
30
+ * @param {HTMLElement} [element] - Element to focus after closing the dialog.
31
+ * @returns {*} Returns the result of UI.alert().
32
+ */
33
+ FormValidation.alert = function (message, element) {
34
+ 'use strict';
35
+ console.warn(
36
+ 'FormValidation.alert() is deprecated. Use UI.alert() instead.',
37
+ );
38
+ return UI.alert(message, element);
39
+ };
40
+
41
+ /**
42
+ * [DEPRECATED] Creates a confirmation dialog using bootbox.
43
+ * Useful for requesting user confirmation before performing an action, such as
44
+ * submitting a form.
45
+ *
46
+ * @deprecated This function is deprecated and will be removed in future
47
+ * versions. Use UI.confirm() instead.
48
+ * @param {HTMLElement} element - Element (form or a) that is being confirmed.
49
+ * @param {string} [message] - Message to show to the user.
50
+ * @param {string} [loading] - Loading message to show during the operation.
51
+ * @returns {*} Returns the result of UI.confirm().
52
+ */
53
+ FormValidation.confirm = function (element, message, loading) {
54
+ 'use strict';
55
+ console.warn(
56
+ 'FormValidation.confirm() is deprecated. Use UI.confirm() instead.',
57
+ );
58
+ return UI.confirm(element, message, loading);
59
+ };
60
+
61
+ /**
62
+ * Performs form field validation, applying different checks according to the
63
+ * assigned CSS classes. The checks include non-emptiness, data type, specific
64
+ * format, among others.
65
+ *
66
+ * @param {string} [formId] - ID of the form to validate. If not provided, all
67
+ * fields with "check" class are validated.
68
+ * @returns {boolean} Returns true if all fields pass the validations, false
69
+ * otherwise.
70
+ */
71
+ FormValidation.check = function (formId) {
72
+ 'use strict';
73
+ let form;
74
+ let fields;
75
+ let isValid = true;
76
+
77
+ try {
78
+ form = formId
79
+ ? document.getElementById(formId)
80
+ : document.querySelector('form');
81
+ fields = form.getElementsByClassName('check');
82
+ } catch (error) {
83
+ console.error(
84
+ 'FormValidation.check: Error searching for fields to validate in the form: ',
85
+ error,
86
+ );
87
+ return false;
88
+ }
89
+
90
+ for (const field of fields) {
91
+ if (field.disabled) {
92
+ continue;
93
+ }
94
+
95
+ try {
96
+ field.value = field.value.trim();
97
+ } catch (error) {
98
+ // Silently fail on fields that are not: input, select, or textarea.
99
+ }
100
+
101
+ const checks = field.className.replace('check ', '').split(' ');
102
+ if (
103
+ checks.indexOf('notempty') === -1 &&
104
+ (typeof Utils !== 'undefined'
105
+ ? Utils.empty(field.value)
106
+ : field.value === null ||
107
+ field.value === undefined ||
108
+ field.value === '')
109
+ ) {
110
+ field.classList.remove('is-invalid');
111
+ field.classList.remove('is-valid');
112
+ continue;
113
+ }
114
+
115
+ for (const check of checks) {
116
+ if (check === '') {
117
+ continue;
118
+ }
119
+
120
+ const checkFunction = FormValidation.check[check];
121
+ if (typeof checkFunction === 'function') {
122
+ const status = checkFunction(field);
123
+ if (status !== true) {
124
+ field.classList.add('is-invalid');
125
+ field.classList.remove('is-valid');
126
+ isValid = false;
127
+
128
+ typeof UI !== 'undefined'
129
+ ? UI.alert(
130
+ status.replace(
131
+ '%s',
132
+ FormValidation.getFieldLabel(field),
133
+ ),
134
+ field,
135
+ )
136
+ : alert(
137
+ status.replace(
138
+ '%s',
139
+ FormValidation.getFieldLabel(field),
140
+ ),
141
+ );
142
+
143
+ try {
144
+ field.select();
145
+ } catch (error) {
146
+ // Silently fail when the select() method is not
147
+ // available on the field.
148
+ }
149
+
150
+ return false;
151
+ } else {
152
+ field.classList.remove('is-invalid');
153
+ field.classList.add('is-valid');
154
+ }
155
+ }
156
+ }
157
+ }
158
+
159
+ if (isValid) {
160
+ form.classList.add('was-validated');
161
+ } else {
162
+ form.classList.remove('was-validated');
163
+ }
164
+
165
+ return true;
166
+ };
167
+
168
+ /**
169
+ * [DEPRECATED] Validates that a field is not empty.
170
+ *
171
+ * @deprecated This function is deprecated and will be removed in future
172
+ * versions. Use FormValidation.check.notempty() instead.
173
+ * @param {HTMLElement} field - Field to validate.
174
+ * @returns {string|boolean} Returns the result of
175
+ * FormValidation.check.notempty().
176
+ */
177
+ FormValidation.check_notempty = function (field) {
178
+ 'use strict';
179
+ console.warn(
180
+ 'FormValidation.check_notempty() is deprecated. Use FormValidation.check.notempty() instead.',
181
+ );
182
+ return FormValidation.check.notempty(field);
183
+ };
184
+
185
+ /**
186
+ * Validates that a field is not empty.
187
+ * Uses the Utils.empty() function to determine if the field is empty.
188
+ *
189
+ * @param {HTMLElement} field - Field to validate.
190
+ * @returns {string|boolean} Returns an error message if the field is empty, or
191
+ * true if it is not.
192
+ */
193
+ FormValidation.check.notempty = function (field) {
194
+ 'use strict';
195
+ if (!(field instanceof HTMLElement)) {
196
+ console.error(
197
+ 'FormValidation.check.notempty: The provided argument is not a valid HTML element.',
198
+ );
199
+ return 'The provided field is not valid!';
200
+ }
201
+ if (field.type === 'checkbox') {
202
+ if (!field.checked) {
203
+ return 'You must check the box: %s!';
204
+ }
205
+ } else if (
206
+ typeof Utils !== 'undefined'
207
+ ? Utils.empty(field.value)
208
+ : field.value === null ||
209
+ field.value === undefined ||
210
+ field.value === ''
211
+ ) {
212
+ return '%s cannot be blank!';
213
+ }
214
+ return true;
215
+ };
216
+
217
+ /**
218
+ * Validates that a field contains an integer.
219
+ * Uses the Utils.isInt() function for validation.
220
+ *
221
+ * @param {HTMLElement} field - Field to validate.
222
+ * @returns {string|boolean} Returns an error message if the field does not
223
+ * contain an integer, or true if it does.
224
+ */
225
+ FormValidation.check.integer = function (field) {
226
+ 'use strict';
227
+ if (!(field instanceof HTMLElement)) {
228
+ console.error(
229
+ 'FormValidation.check.integer: The provided argument is not a valid HTML element.',
230
+ );
231
+ return 'The provided field is not valid!';
232
+ }
233
+ if (
234
+ typeof Utils !== 'undefined'
235
+ ? !Utils.isInt(field.value)
236
+ : !Number.isInteger(parseFloat(field.value))
237
+ ) {
238
+ return '%s must be an integer!';
239
+ }
240
+ field.value = parseInt(field.value, 10);
241
+ return true;
242
+ };
243
+
244
+ /**
245
+ * [DEPRECATED] Validates that a field contains a real number (integer or
246
+ * decimal).
247
+ *
248
+ * @deprecated This function is deprecated and will be removed in future
249
+ * versions. Use FormValidation.check.real() instead.
250
+ * @param {HTMLElement} field - Field to validate.
251
+ * @returns {string|boolean} Returns the result of
252
+ * FormValidation.check.real().
253
+ */
254
+ FormValidation.check_real = function (field) {
255
+ 'use strict';
256
+ console.warn(
257
+ 'FormValidation.check_real() is deprecated. Use FormValidation.check.real() instead.',
258
+ );
259
+ return FormValidation.check.real(field);
260
+ };
261
+
262
+ /**
263
+ * Validates that a field contains a real number (integer or decimal).
264
+ * Uses the Utils.isInt() and Utils.isFloat() functions for validation.
265
+ *
266
+ * @param {HTMLElement} field - Field to validate.
267
+ * @returns {string|boolean} Returns an error message if the field does not
268
+ * contain a real number, or true if it does.
269
+ */
270
+ FormValidation.check.real = function (field) {
271
+ 'use strict';
272
+ if (!(field instanceof HTMLElement)) {
273
+ console.error(
274
+ 'FormValidation.check.real: The provided argument is not a valid HTML element.',
275
+ );
276
+ return 'The provided field is not valid!';
277
+ }
278
+ field.value = field.value.replace(',', '.');
279
+ if (
280
+ typeof Utils !== 'undefined'
281
+ ? !Utils.isInt(field.value) && !Utils.isFloat(field.value)
282
+ : !Number.isInteger(parseFloat(field.value)) &&
283
+ !field.value.includes('.')
284
+ ) {
285
+ return '%s must be an integer or decimal number!';
286
+ }
287
+ field.value = parseFloat(field.value);
288
+ return true;
289
+ };
290
+
291
+ /**
292
+ * [DEPRECATED] Validates that a field contains a valid email address.
293
+ *
294
+ * @deprecated This function is deprecated and will be removed in future
295
+ * versions. Use FormValidation.check.email() instead.
296
+ * @param {HTMLElement} field - Field to validate.
297
+ * @returns {string|boolean} Returns the result of
298
+ * FormValidation.check.email().
299
+ */
300
+ FormValidation.check_email = function (field) {
301
+ 'use strict';
302
+ console.warn(
303
+ 'FormValidation.check_email() is deprecated. Use FormValidation.check.email() instead.',
304
+ );
305
+ return FormValidation.check.email(field);
306
+ };
307
+
308
+ /**
309
+ * Validates that a field contains a valid email address.
310
+ * Uses a regular expression for validation.
311
+ *
312
+ * @param {HTMLElement} field - Field to validate.
313
+ * @returns {string|boolean} Returns an error message if the field does not
314
+ * contain a valid email address, or true if it does.
315
+ */
316
+ FormValidation.check.email = function (field) {
317
+ 'use strict';
318
+ const emailRegex =
319
+ /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
320
+ if (!(field instanceof HTMLElement)) {
321
+ console.error(
322
+ 'FormValidation.check.email: The provided argument is not a valid HTML element.',
323
+ );
324
+ return 'The provided field is not valid!';
325
+ }
326
+ if (!emailRegex.test(field.value)) {
327
+ return '%s is not valid!';
328
+ }
329
+ return true;
330
+ };
331
+
332
+ /**
333
+ * Validates that a field contains one or several valid email addresses.
334
+ * Uses a regular expression for validation.
335
+ *
336
+ * @param {HTMLElement} field - Field to validate.
337
+ * @returns {string|boolean} Returns an error message if the field does not
338
+ * contain valid emails, or true if it does.
339
+ */
340
+ FormValidation.check.emails = function (field) {
341
+ 'use strict';
342
+ const emailRegex =
343
+ /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
344
+ if (!(field instanceof HTMLElement)) {
345
+ console.error(
346
+ 'FormValidation.check.emails: The provided argument is not a valid HTML element.',
347
+ );
348
+ return 'The provided field is not valid!';
349
+ }
350
+ field.value = field.value.replace(/ /g, '');
351
+ const emails = field.value
352
+ .replace(/\n/g, ';')
353
+ .replace(/,/g, ';')
354
+ .split(';');
355
+ for (let i = 0; i < emails.length; i++) {
356
+ if (!emailRegex.test(emails[i])) {
357
+ return '%s is not valid!';
358
+ }
359
+ }
360
+ return true;
361
+ };
362
+
363
+ /**
364
+ * Validates that a field contains a date in YYYY-MM-DD format.
365
+ * Uses a regular expression for validation.
366
+ *
367
+ * @param {HTMLElement} field - Field to validate.
368
+ * @returns {string|boolean} Returns an error message if the field does not
369
+ * contain a valid date, or true if it does.
370
+ */
371
+ FormValidation.check.date = function (field) {
372
+ 'use strict';
373
+ const dateRegex = /^\d{4}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$/;
374
+ if (!(field instanceof HTMLElement)) {
375
+ console.error(
376
+ 'FormValidation.check.date: The provided argument is not a valid HTML element.',
377
+ );
378
+ return 'The provided field is not valid!';
379
+ }
380
+ if (!dateRegex.test(field.value)) {
381
+ return '%s must be in YYYY-MM-DD format!';
382
+ }
383
+ return true;
384
+ };
385
+
386
+ /**
387
+ * Validates that a field contains a phone number in specific format.
388
+ * Example of valid format: +56 9 87654321
389
+ * Uses a regular expression for validation.
390
+ *
391
+ * @param {HTMLElement} field - Field to validate.
392
+ * @returns {string|boolean} Returns an error message if the field does not
393
+ * contain a valid phone number, or true if it does.
394
+ */
395
+ FormValidation.check.telephone = function (field) {
396
+ 'use strict';
397
+ const phoneRegex =
398
+ /^(?:\+\d{1,4}\s?)?(?:(\d{1,3})[\s-]?)?(\d{3,4})[\s-]?(\d{4})$/;
399
+ if (!(field instanceof HTMLElement)) {
400
+ console.error(
401
+ 'FormValidation.check.telephone: The provided argument is not a valid HTML element.',
402
+ );
403
+ return 'The provided field is not valid!';
404
+ }
405
+ if (!phoneRegex.test(field.value)) {
406
+ return '%s must be a valid phone number!';
407
+ }
408
+ return true;
409
+ };
410
+
411
+ /**
412
+ * [DEPRECATED] Validates that a field contains a valid Chilean RUT.
413
+ *
414
+ * @deprecated This function is deprecated and will be removed in future
415
+ * versions. Use FormValidation.check.rut() instead.
416
+ * @param {HTMLElement} field - Field to validate.
417
+ * @returns {string|boolean} Returns the result of
418
+ * FormValidation.check.rut().
419
+ */
420
+ FormValidation.check_rut = function (field) {
421
+ 'use strict';
422
+ console.warn(
423
+ 'FormValidation.check_rut() is deprecated. Use FormValidation.check.rut() instead.',
424
+ );
425
+ return FormValidation.check.rut(field);
426
+ };
427
+
428
+ /**
429
+ * Validates that a field contains a valid Chilean RUT.
430
+ * Uses the ValidationL10nCl.rut() function to determine if the RUT is valid or not.
431
+ *
432
+ * @param {HTMLElement} field - Field to validate.
433
+ * @returns {string|boolean} Returns an error message if the RUT is not valid,
434
+ * or true if it is.
435
+ */
436
+ FormValidation.check.rut = function (field) {
437
+ 'use strict';
438
+ if (!(field instanceof HTMLElement)) {
439
+ console.error(
440
+ 'FormValidation.check.rut: The provided argument is not a valid HTML element.',
441
+ );
442
+ return 'The provided field is not valid!';
443
+ }
444
+ const rut =
445
+ typeof ValidationL10nCl !== 'undefined'
446
+ ? ValidationL10nCl.rut(field.value, true)
447
+ : false;
448
+ if (rut === false) {
449
+ return '%s is not valid!';
450
+ }
451
+ field.value = rut;
452
+ return true;
453
+ };
454
+
455
+ /**
456
+ * Gets the label of a form field.
457
+ * First looks for a label element within the form group where the field is
458
+ * located.
459
+ * If no label is found, uses the field's placeholder.
460
+ * If neither label nor placeholder is available, uses the field's name.
461
+ *
462
+ * @param {HTMLElement} field - Form field from which the label is wanted.
463
+ * @returns {string} Text of the label associated with the field, its placeholder,
464
+ * or its name.
465
+ */
466
+ FormValidation.getFieldLabel = function (field) {
467
+ 'use strict';
468
+ const formGroup = field.parentNode.parentNode;
469
+ const label_element = formGroup.querySelector('label');
470
+ let label;
471
+ if (label_element) {
472
+ label = label_element.textContent.replace('* ', '');
473
+ } else {
474
+ label = field.placeholder;
475
+ }
476
+ return label ? label : field.name;
477
+ };
478
+
479
+ // Export module for use in node.js.
480
+ if (typeof module === 'object' && module.exports) {
481
+ module.exports = FormValidation;
482
+ }
package/src/form.js ADDED
@@ -0,0 +1,45 @@
1
+ /*! Form functions | (c) 2025 Derafu DEV | MIT */
2
+
3
+ // Import all form modules.
4
+ // Note: In a browser environment, these modules should be loaded before this
5
+ // file. The modules are: FormValidation, FormFields, FormTables.
6
+
7
+ // Main object where all form functions will be assigned for backward
8
+ // compatibility.
9
+ const Form = {};
10
+
11
+ // Import FormValidation functions.
12
+ if (typeof FormValidation !== 'undefined') {
13
+ Form.loading = FormValidation.loading;
14
+ Form.alert = FormValidation.alert;
15
+ Form.confirm = FormValidation.confirm;
16
+ Form.check = FormValidation.check;
17
+ Form.check_notempty = FormValidation.check_notempty;
18
+ Form.check_real = FormValidation.check_real;
19
+ Form.check_email = FormValidation.check_email;
20
+ Form.check_rut = FormValidation.check_rut;
21
+ Form.getFieldLabel = FormValidation.getFieldLabel;
22
+ }
23
+
24
+ // Import FormFields functions.
25
+ if (typeof FormFields !== 'undefined') {
26
+ Form.post = FormFields.post;
27
+ Form.showPassword = FormFields.showPassword;
28
+ Form.growup = FormFields.growup;
29
+ Form.fixFields = FormFields.fixFields;
30
+ Form.checkboxesSet = FormFields.checkboxesSet;
31
+ Form.removeOptions = FormFields.removeOptions;
32
+ Form.addOptions = FormFields.addOptions;
33
+ }
34
+
35
+ // Import FormTables functions.
36
+ if (typeof FormTables !== 'undefined') {
37
+ Form.addJS = FormTables.addJS;
38
+ Form.delJS = FormTables.delJS;
39
+ Form.updateTablecheck = FormTables.updateTablecheck;
40
+ }
41
+
42
+ // Export module for use in Node.js.
43
+ if (typeof module === 'object' && module.exports) {
44
+ module.exports = Form;
45
+ }