13693261870
2022-09-16 354b3dbfbffb3df45212a2a44dbbf48b4acc2594
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* http://plugins.jquery.com/validate */
;(function(defaults, $, window, undefined) {
 
    var
 
        type = ['input:not([type]),input[type="color"],input[type="date"],input[type="datetime"],input[type="datetime-local"],input[type="email"],input[type="file"],input[type="hidden"],input[type="month"],input[type="number"],input[type="password"],input[type="range"],input[type="search"],input[type="tel"],input[type="text"],input[type="time"],input[type="url"],input[type="week"],textarea', 'select', 'input[type="checkbox"],input[type="radio"]'],
 
        // All field types
        allTypes = type.join(','),
 
        extend = {},
 
        // Method to validate each fields
        validateField = function(event, options) {
 
            var
 
                // Field status
                status = {
                    pattern : true,
                    conditional : true,
                    required : true
                },
 
                // Current field
                field = $(this),
 
                // Current field value
                fieldValue = field.val() || '',
 
                // An index of extend
                fieldValidate = field.data('validate'),
 
                // A validation object (jQuery.fn.validateExtend)
                validation = fieldValidate !== undefined ? extend[fieldValidate] : {},
 
                // One index or more separated for spaces to prepare the field value
                fieldPrepare = field.data('prepare') || validation.prepare,
 
                // A regular expression to validate field value
                fieldPattern = (field.data('pattern') || ($.type(validation.pattern) == 'regexp' ? validation.pattern : /(?:)/)),
 
                // Is case sensitive? (Boolean)
                fieldIgnoreCase = field.attr('data-ignore-case') || field.data('ignoreCase') || validation.ignoreCase,
 
                // A field mask
                fieldMask = field.data('mask') || validation.mask,
 
                // A index in the conditional object containing a function to validate the field value
                fieldConditional = field.data('conditional') || validation.conditional,
 
                // Is required?
                fieldRequired = field.data('required'),
 
                // The description element id
                fieldDescribedby = field.data('describedby') || validation.describedby,
 
                // An index of description object
                fieldDescription = field.data('description') || validation.description,
 
                // Trim spaces?
                fieldTrim = field.data('trim'),
 
                reTrue = /^(true|)$/i,
 
                reFalse = /^false$/i,
 
                // The description object
                fieldDescription = $.isPlainObject(fieldDescription) ? fieldDescription : (options.description[fieldDescription] || {}),
 
                name = 'validate';
 
            fieldRequired = fieldRequired != '' ? (fieldRequired || !!validation.required) : true;
 
            fieldTrim = fieldTrim != '' ? (fieldTrim || !!validation.trim) : true;
 
            // Trim spaces?
            if(reTrue.test(fieldTrim)) {
 
                fieldValue = $.trim(fieldValue);
            }
 
            // The fieldPrepare is a function?
            if($.isFunction(fieldPrepare)) {
 
                // Updates the fieldValue variable
                fieldValue = String(fieldPrepare.call(field, fieldValue));
            } else {
 
                // Is a function?
                if($.isFunction(options.prepare[fieldPrepare])) {
 
                    // Updates the fieldValue variable
                    fieldValue = String(options.prepare[fieldPrepare].call(field, fieldValue));
                }
            }
 
            // Is not RegExp?
            if($.type(fieldPattern) != 'regexp') {
 
                fieldIgnoreCase = !reFalse.test(fieldIgnoreCase);
 
                // Converts to RegExp
                fieldPattern = fieldIgnoreCase ? RegExp(fieldPattern, 'i') : RegExp(fieldPattern);
            }
 
            // The conditional exists?
            if(fieldConditional != undefined) {
 
                // The fieldConditional is a function?
                if($.isFunction(fieldConditional)) {
 
                    status.conditional = !!fieldConditional.call(field, fieldValue, options);
                } else {
 
                    var
 
                        // Splits the conditionals in an array
                        conditionals = fieldConditional.split(/[\s\t]+/);
 
                    // Each conditional
                    for(var counter = 0, len = conditionals.length; counter < len; counter++) {
 
                        if(options.conditional.hasOwnProperty(conditionals[counter]) && !options.conditional[conditionals[counter]].call(field, fieldValue, options)) {
 
                            status.conditional = false;
                        }
                    }
                }
            }
 
            fieldRequired = reTrue.test(fieldRequired);
 
            // Is required?
            if(fieldRequired) {
 
                // Verifies the field type
                if(field.is(type[0] + ',' + type[1])) {
 
                    // Is empty?
                    if(!fieldValue.length > 0) {
 
                        status.required = false;
                    }
                } else if(field.is(type[2])) {
 
                    if(field.is('[name]')) {
 
                        // Is checked?
                        if($('[name="' + field.prop('name') + '"]:checked').length == 0) {
 
                            status.required = false;
                        }
                    } else {
 
                        status.required = field.is(':checked');
                    }
                }
            }
 
            // Verifies the field type
            if(field.is(type[0])) {
 
                // Test the field value pattern
                if(fieldPattern.test(fieldValue)) {
 
                    // If the event type is not equals to keyup
                    if(event.type != 'keyup' && fieldMask !== undefined) {
 
                        var matches = fieldValue.match(fieldPattern);
 
                        // Each characters group
                        for(var i = 0, len = matches.length; i < len; i++) {
 
                            // Replace the groups
                            fieldMask = fieldMask.replace(RegExp('\\$\\{' + i + '(?::`([^`]*)`)?\\}', 'g'), (matches[i] !== undefined ? matches[i] : '$1'));
                        }
 
                        fieldMask = fieldMask.replace(/\$\{\d+(?::`([^`]*)`)?\}/g, '$1');
 
                        // Test the field value pattern
                        if(fieldPattern.test(fieldMask)) {
 
                            // Update the field value
                            field.val(fieldMask);
                        }
                    }
                } else {
 
                    // If the field is required
                    if(fieldRequired) {
 
                        status.pattern = false;
                    } else {
 
                        if(fieldValue.length > 0) {
 
                            status.pattern = false;
                        }
                    }
                }
            }
 
            var
 
                describedby = $('[id="' + fieldDescribedby +'"]'),
 
                log = fieldDescription.valid;
 
            if(describedby.length > 0 && event.type != 'keyup') {
 
                if(!status.required) {
 
                    log = fieldDescription.required;
                } else if(!status.pattern) {
 
                    log = fieldDescription.pattern;
                } else if(!status.conditional) {
 
                    log = fieldDescription.conditional;
                }
 
                describedby.html(log || '');
            }
 
            if(typeof(validation.each) == 'function') {
 
                validation.each.call(field, event, status, options);
            }
 
            // Call the eachField callback
            options.eachField.call(field, event, status, options);
 
            // If the field is valid
            if(status.required && status.pattern && status.conditional) {
 
                // If WAI-ARIA is enabled
                if(!!options.waiAria) {
 
                    field.prop('aria-invalid', false);
                }
 
                if(typeof(validation.valid) == 'function') {
 
                    validation.valid.call(field, event, status, options);
                }
 
                // Call the eachValidField callback
                options.eachValidField.call(field, event, status, options);
            } else {
 
                // If WAI-ARIA is enabled
                if(!!options.waiAria) {
 
                    field.prop('aria-invalid', true);
                }
 
                if(typeof(validation.invalid) == 'function') {
 
                    validation.invalid.call(field, event, status, options);
                }
 
                // Call the eachInvalidField callback
                options.eachInvalidField.call(field, event, status, options);
            }
 
            // Returns the field status
            return status;
        };
 
    $.extend({
 
        // Method to extends validations
        validateExtend : function(options) {
 
            return $.extend(extend, options);
        },
 
        // Method to change the default properties of jQuery.fn.validate method
        validateSetup : function(options) {
 
            return $.extend(defaults, options);
        }
    }).fn.extend({
 
        // Method to validate forms
        validate : function(options) {
 
            options = $.extend({}, defaults, options);
 
            return $(this).validateDestroy().each(function() {
 
                var form = $(this);
 
                // This is a form?
                if(form.is('form')) {
 
                    form.data(name, {
                        options : options
                    });
 
                    var
 
                        fields = form.find(allTypes),
 
                        // Events namespace
                        namespace = options.namespace;
 
                    if(form.is('[id]')) {
 
                        fields = fields.add('[form="' + form.prop('id') + '"]').filter(allTypes);
                    }
 
                    fields = fields.filter(options.filter);
 
                    // If onKeyup is enabled
                    if(!!options.onKeyup) {
 
                        fields.filter(type[0]).on('keyup.' + namespace, function(event) {
 
                            validateField.call(this, event, options);
                        });
                    }
 
                    // If onBlur is enabled
                    if(!!options.onBlur) {
 
                        fields.on('blur.' + namespace, function(event) {
 
                            validateField.call(this, event, options);
                        });
                    }
 
                    // If onChange is enabled
                    if(!!options.onChange) {
 
                        fields.on('change.' + namespace, function(event) {
 
                            validateField.call(this, event, options);
                        });
                    }
 
                    // If onSubmit is enabled
                    if(!!options.onSubmit) {
 
                        form.on('submit.' + namespace, function(event) {
 
                            var formValid = true;
 
                            fields.each(function() {
 
                                var status = validateField.call(this, event, options);
 
                                if(!status.pattern || !status.conditional || !status.required) {
 
                                    formValid = false;
                                }
                            });
 
                            // If form is valid
                            if(formValid) {
 
                                // Send form?
                                if(!options.sendForm) {
 
                                    event.preventDefault();
                                }
 
                                // Is a function?
                                if($.isFunction(options.valid)) {
 
                                    options.valid.call(form, event, options);
                                }
                            } else {
 
                                event.preventDefault();
                event.stopImmediatePropagation();
 
                                // Is a function?
                                if($.isFunction(options.invalid)) {
 
                                    options.invalid.call(form, event, options);
                                }
                            }
                        });
                    }
                }
            });
        },
 
        // Method to destroy validations
        validateDestroy : function() {
 
            var
 
                form = $(this),
 
                dataValidate = form.data(name);
 
            // If this is a form
            if(form.is('form') && $.isPlainObject(dataValidate) && typeof(dataValidate.options.nameSpace) == 'string') {
 
                var fields = form.removeData(name).find(allTypes).add(form);
 
                if(form.is('[id]')) {
 
                    fields = fields.add($('[form="' + form.prop('id') + '"]').filter(allTypes));
                }
 
                fields.off('.' + dataValidate.options.nameSpace);
            }
 
            return form;
        }
    });
})({
 
    // Send form if is valid?
    sendForm : true,
 
    // Use WAI-ARIA properties
    waiAria : true,
 
    // Validate on submit?
    onSubmit : true,
 
    // Validate on onKeyup?
    onKeyup : false,
 
    // Validate on onBlur?
    onBlur : false,
 
    // Validate on onChange?
    onChange : false,
 
    // Default namespace
    nameSpace : 'validate',
 
    // Conditional functions
    conditional : {},
 
    // Prepare functions
    prepare : {},
 
    // Fields descriptions
    description : {},
 
    // Callback
    eachField : $.noop,
 
    // Callback
    eachInvalidField : $.noop,
 
    // Callback
    eachValidField : $.noop,
 
    // Callback
    invalid : $.noop,
 
    // Callback
    valid : $.noop,
 
    // A fielter to the fields
    filter : '*'
}, jQuery, window);