PageSourceSearch

https://codeplay.com/assets/js/jquery.simpleformvalidator.js?v=v6

js codeplay.com collected 2026-10-02 03:12:08 UTC 3,532 bytes, 104 lines download raw bytes

1(function ($) {
2    $.fn.SimpleFormValidator = function(onValidCallbackFn, onInvalidCallbackFn) {
3        const fElement = $(this);
4        const fThis = this;
5
6        // This is the minimum length of a input before it will attempt to show validation failure
7        // (will still run callback)
8        const fValidationLengthThreshold = 3;
9
10        /**
11         * Validate all inputs.
12         */
13        this.validateAll = function () {
14            let allFormsValid = true;
15
16            fElement.find(':input').each(function() {
17                const currentInput = $(this);
18                const isInputValid = fThis.isValid(currentInput);
19
20                // Form isn't valid, validate it
21                if (!isInputValid) {
22                    if (currentInput.val().length >= fValidationLengthThreshold) {
23                        if (currentInput.attr("id")) {
24                            fElement.find("label[for='" + currentInput.attr("id") + "']").css("color", "red");
25                        }
26                    }
27
28                    allFormsValid = false;
29                    return;
30                } else {
31                    // Clear any previous feedback
32                    fElement.find("label[for='" + currentInput.attr("id") + "']").css("color", "inherit");
33                }
34            });
35
36            if(fElement.data('is-valid')) {
37                if (fElement.data('is-valid') !== "true") {
38                    allFormsValid = false;
39                }
40            }
41
42            if (allFormsValid) {
43                fElement.find('button[type="submit"]').prop("disabled", false);
44
45                if(onValidCallbackFn) {
46                    onValidCallbackFn(fElement);
47                }
48            } else {
49                fElement.find('button[type="submit"]').prop("disabled", true);
50
51                if(onInvalidCallbackFn) {
52                    onInvalidCallbackFn(fElement);
53                }
54            }
55        };
56
57        /**
58         * Validate a single input.
59         * @param inputElement
60         */
61        this.isValid = function (inputElement) {
62            const elementValue = inputElement.val();
63
64            // Check if field is required or not
65            if (inputElement.prop("required")) {
66                if (elementValue.length === 0) {
67                    return false;
68                }
69            }
70
71            // Validate email types
72            if (inputElement.prop("type") === 'email') {
73                const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
74                if(!re.test(elementValue)) {
75                    return false;
76                }
77            }
78
79            // Validate min length
80            if (inputElement.attr("minlength") !== undefined) {
81                if (elementValue.length < inputElement.attr("minlength")) {
82                    return false;
83                }
84            }
85
86            // Validate max length
87            if (inputElement.attr("maxlength") !== undefined) {
88                if (elementValue.length > inputElement.attr("maxlength")) {
89                    return false;
90                }
91            }
92
93            return true;
94        };
95
96        // Detect any changes and re-validate all inputs
97        fElement.on('change keyup paste', ':input', function(e) {
98            fThis.validateAll();
99        });
100
101        // Perform initial validation
102        fThis.validateAll();
103    };
104})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.