PageSourceSearch

https://bobr.by/assets/v367/js/plugins/livevalidation.js

js bobr.by collected 2026-10-01 11:44:03 UTC 22,188 bytes, 617 lines download raw bytes

1// LiveValidation 1.3.2  (standalone version) (updated by MOZG)
2// Copyright (c) 2007-2008 Alec Hill (www.livevalidation.com)
3// LiveValidation is licensed under the terms of the MIT License
4var LiveValidation = function(B, A) {
5    this.initialize(B, A);
6};
7LiveValidation.VERSION = "1.3 standalone";
8LiveValidation.TEXTAREA = 1;
9LiveValidation.TEXT = 2;
10LiveValidation.PASSWORD = 3;
11LiveValidation.CHECKBOX = 4;
12LiveValidation.SELECT = 5;
13LiveValidation.FILE = 6;
14LiveValidation.massValidate = function(C) {
15    var D = true;
16    for (var B = 0, A = C.length; B < A; ++B) {
17        var E = C[B].validate();
18        if (D) {
19            D = E;
20        }
21    }
22    return D;
23};
24LiveValidation.prototype = {
25    validClass: "LV_valid",
26    invalidClass: "LV_invalid",
27    messageClass: "LV_validation_message",
28    validFieldClass: "LV_valid_field",
29    invalidFieldClass: "LV_invalid_field",
30    force_check_empty: false,
31    initialize: function(D, C) {
32        var A = this;
33        if (!D) {
34            throw new Error("LiveValidation::initialize - No element reference or element id has been provided!");
35        }
36        this.element = D.nodeName ? D : document.getElementById(D);
37        if (!this.element) {
38            throw new Error("LiveValidation::initialize - No element with reference or id of '" + D + "' exists!");
39        }
40        this.validations = [];
41        this.elementType = this.getElementType();
42        this.form = this.element.form;
43        var B = C || {};
44        this.validMessage = B.validMessage || "";
45        var E = B.insertAfterWhatNode || this.element;
46        this.insertAfterWhatNode = E.nodeType ? E : document.getElementById(E);
47        this.onValid = B.onValid || function() {
48            this.insertMessage(this.createMessageSpan());
49            this.addFieldClass();
50        };
51        this.onInvalid = B.onInvalid || function() {
52            this.insertMessage(this.createMessageSpan());
53            this.addFieldClass();
54        };
55        this.onlyOnBlur = B.onlyOnBlur || false;
56        this.wait = B.wait || 0;
57        this.onlyOnSubmit = B.onlyOnSubmit || false;
58        if (this.form) {
59            this.formObj = LiveValidationForm.getInstance(this.form);
60            this.formObj.addField(this);
61        }
62        this.oldOnFocus = this.element.onfocus || function() {};
63        this.oldOnBlur = this.element.onblur || function() {};
64        this.oldOnClick = this.element.onclick || function() {};
65        this.oldOnChange = this.element.onchange || function() {};
66        this.oldOnKeyup = this.element.onkeyup || function() {};
67        this.element.onfocus = function(F) {
68            A.doOnFocus(F);
69            return A.oldOnFocus.call(this, F);
70        };
71        if (!this.onlyOnSubmit) {
72            switch (this.elementType) {
73                case LiveValidation.CHECKBOX:
74                    this.element.onclick = function(F) {
75                        A.validate();
76                        return A.oldOnClick.call(this, F);
77                    };
78                case LiveValidation.SELECT:
79                case LiveValidation.FILE:
80                    this.element.onchange = function(F) {
81                        A.validate();
82                        return A.oldOnChange.call(this, F);
83                    };
84                    break;
85                default:
86                    if (!this.onlyOnBlur) {
87                        this.element.onkeyup = function(F) {
88                            A.deferValidation();
89                            return A.oldOnKeyup.call(this, F);
90                        };
91                    }
92                    this.element.onblur = function(F) {
93                        A.doOnBlur(F);
94                        return A.oldOnBlur.call(this, F);
95                    };
96            }
97        }
98    },
99    destroy: function() {
100        if (this.formObj) {
101            this.formObj.removeField(this);
102            this.formObj.destroy();
103        }
104        this.element.onfocus = this.oldOnFocus;
105        if (!this.onlyOnSubmit) {
106            switch (this.elementType) {
107                case LiveValidation.CHECKBOX:
108                    this.element.onclick = this.oldOnClick;
109                case LiveValidation.SELECT:
110                case LiveValidation.FILE:
111                    this.element.onchange = this.oldOnChange;
112                    break;
113                default:
114                    if (!this.onlyOnBlur) {
115                        this.element.onkeyup = this.oldOnKeyup;
116                    }
117                    this.element.onblur = this.oldOnBlur;
118            }
119        }
120        this.validations = [];
121        this.removeMessageAndFieldClass();
122    },
123    add: function(A, B) {
124        this.validations.push({
125            type: A,
126            params: B || {}
127        });
128        return this;
129    },
130    remove: function(B, D) {
131        var E = false;
132        for (var C = 0, A = this.validations.length; C < A; C++) {
133            if (this.validations[C].type == B) {
134                if (this.validations[C].params == D) {
135                    E = true;
136                    break;
137                }
138            }
139        }
140        if (E) {
141            this.validations.splice(C, 1);
142        }
143        return this;
144    },
145    deferValidation: function(B) {
146        if (this.wait >= 300) {
147            this.removeMessageAndFieldClass();
148        }
149        var A = this;
150        if (this.timeout) {
151            clearTimeout(A.timeout);
152        }
153        this.timeout = setTimeout(function() {
154            A.validate();
155        }, A.wait);
156    },
157    doOnBlur: function(A) {
158        this.focused = false;
159        this.validate(A);
160    },
161    doOnFocus: function(A) {
162        this.focused = true;
163        this.removeMessageAndFieldClass();
164    },
165    getElementType: function() {
166        switch (true) {
167            case (this.element.nodeName.toUpperCase() == "TEXTAREA"):
168                return LiveValidation.TEXTAREA;
169            case (this.element.nodeName.toUpperCase() == "INPUT" && this.element.type.toUpperCase() == "TEXT"):
170                return LiveValidation.TEXT;
171            case (this.element.nodeName.toUpperCase() == "INPUT" && this.element.type.toUpperCase() == "PASSWORD"):
172                return LiveValidation.PASSWORD;
173            case (this.element.nodeName.toUpperCase() == "INPUT" && this.element.type.toUpperCase() == "CHECKBOX"):
174                return LiveValidation.CHECKBOX;
175            case (this.element.nodeName.toUpperCase() == "INPUT" && this.element.type.toUpperCase() == "FILE"):
176                return LiveValidation.FILE;
177            case (this.element.nodeName.toUpperCase() == "SELECT"):
178                return LiveValidation.SELECT;
179            case (this.element.nodeName.toUpperCase() == "INPUT"):
180                //debugger;
181                return LiveValidation.TEXT;
182                //throw new Error("LiveValidation::getElementType - Cannot use LiveValidation on an " + this.element.type + " input!");
183            default:
184                throw new Error("LiveValidation::getElementType - Element must be an input, select, or textarea!");
185        }
186    },
187    doValidations: function() {
188        this.validationFailed = false;
189        for (var C = 0, A = this.validations.length; C < A; ++C) {
190            var B = this.validations[C];
191            switch (B.type) {
192                case Validate.Presence:
193                case Validate.Confirmation:
194                case Validate.Acceptance:
195                    this.displayMessageWhenEmpty = true;
196                    this.validationFailed = !this.validateElement(B.type, B.params, this);
197                    break;
198                default:
199                    this.validationFailed = !this.validateElement(B.type, B.params, this);
200                    break;
201            }
202            if (this.validationFailed) {
203                return false;
204            }
205        }
206        this.message = this.validMessage;
207        return true;
208    },
209    validateElement: function(A, C, owner) {
210        var D = (this.elementType == LiveValidation.SELECT) ? this.element.options[this.element.selectedIndex].value : this.element.value;
211        if (A == Validate.Acceptance) {
212            if (this.elementType != LiveValidation.CHECKBOX) {
213                throw new Error("LiveValidation::validateElement - Element to validate acceptance must be a checkbox!");
214            }
215            D = this.element.checked;
216        }
217        var E = true;
218        try {
219            A(D, C, owner);
220        } catch (B) {
221            if (B instanceof Validate.Error) {
222
223                if (D !== "" || (D === "" && this.displayMessageWhenEmpty) || B.force_check_empty) {
224                    //if (B.force_check_empty)debugger;
225                    this.validationFailed = true;
226                    this.message = B.message;
227                    this.force_check_empty = true;
228                    E = false;
229                }
230            } else {
231                throw B;
232            }
233        } finally {
234            return E;
235        }
236    },
237    validate: function() {
238        if (!this.element.disabled) {
239            var A = this.doValidations();
240            if (A) {
241                this.onValid();
242                return true;
243            } else {
244                this.onInvalid();
245                return false;
246            }
247        } else {
248            return true;
249        }
250    },
251    enable: function() {
252        this.element.disabled = false;
253        return this;
254    },
255    disable: function() {
256        this.element.disabled = true;
257        this.removeMessageAndFieldClass();
258        return this;
259    },
260    createMessageSpan: function() {
261        var A = document.createElement("p");
262        var B = document.createTextNode(this.message);
263        A.appendChild(B);
264        return A;
265    },
266    insertMessage: function(B) {
267        this.removeMessage();
268        if ((this.displayMessageWhenEmpty && (this.elementType == LiveValidation.CHECKBOX || this.element.value == "")) || this.element.value != "") {
269            var A = this.validationFailed ? this.invalidClass : this.validClass;
270            B.className += " " + this.messageClass + " " + A;
271            if (this.insertAfterWhatNode.nextSibling) {
272                if (this.insertAfterWhatNode.parentNode === null) {
273                    //debugger;
274                    console.log('LV warning : field with id=' + this.insertAfterWhatNode.id);
275                } else {
276                    this.insertAfterWhatNode.parentNode.insertBefore(B, this.insertAfterWhatNode.nextSibling);
277                }
278
279            } else {
280                if (this.insertAfterWhatNode.parentNode === null) {
281                    console.log('LV warning : field with id=' + this.insertAfterWhatNode.id);
282                    //debugger;
283                } else {
284                    this.insertAfterWhatNode.parentNode.appendChild(B);
285                }
286
287            }
288        }
289    },
290    addFieldClass: function() {
291        this.removeFieldClass();
292        if (!this.validationFailed) {
293            if (this.displayMessageWhenEmpty || this.element.value != "") {
294                if (this.element.className.indexOf(this.validFieldClass) == -1) {
295                    this.element.className += " " + this.validFieldClass;
296                }
297            }
298        } else {
299            if (this.element.className.indexOf(this.invalidFieldClass) == -1) {
300                this.element.className += " " + this.invalidFieldClass;
301            }
302        }
303    },
304    removeMessage: function() {
305        var A;
306        var B = this.insertAfterWhatNode;
307        while (B.nextSibling) {
308            if (B.nextSibling.nodeType === 1) {
309                A = B.nextSibling;
310                break;
311            }
312            B = B.nextSibling;
313        }
314        if (A && A.className.indexOf(this.messageClass) != -1) {
315            this.insertAfterWhatNode.parentNode.removeChild(A);
316        }
317    },
318    removeFieldClass: function() {
319        if (this.element.className.indexOf(this.invalidFieldClass) != -1) {
320            this.element.className = this.element.className.split(this.invalidFieldClass).join("");
321        }
322        if (this.element.className.indexOf(this.validFieldClass) != -1) {
323            this.element.className = this.element.className.split(this.validFieldClass).join(" ");
324        }
325    },
326    removeMessageAndFieldClass: function() {
327        this.removeMessage();
328        this.removeFieldClass();
329    }
330};
331
332var LiveValidationForm = function(A) {
333    this.initialize(A);
334};
335LiveValidationForm.instances = {};
336LiveValidationForm.getInstance = function(A) {
337    var B = Math.random() * Math.random();
338    if (!A.id) {
339        A.id = "formId_" + B.toString().replace(/\./, "") + new Date().valueOf();
340    }
341    if (!LiveValidationForm.instances[A.id]) {
342        LiveValidationForm.instances[A.id] = new LiveValidationForm(A);
343    }
344    return LiveValidationForm.instances[A.id];
345};
346LiveValidationForm.prototype = {
347    initialize: function(B) {
348        this.name = B.id;
349        this.element = B;
350        this.fields = [];
351        this.oldOnSubmit = this.element.onsubmit || function() {};
352        var A = this;
353        this.element.onsubmit = function(C) {
354            return (LiveValidation.massValidate(A.fields)) ? A.oldOnSubmit.call(this, C || window.event) !== false : false;
355        };
356    },
357    addField: function(A) {
358        this.fields.push(A);
359    },
360    removeField: function(C) {
361        var D = [];
362        for (var B = 0, A = this.fields.length; B < A; B++) {
363            if (this.fields[B] !== C) {
364                D.push(this.fields[B]);
365            }
366        }
367        this.fields = D;
368    },
369    destroy: function(A) {
370        if (this.fields.length != 0 && !A) {
371            return false;
372        }
373        this.element.onsubmit = this.oldOnSubmit;
374        LiveValidationForm.instances[this.name] = null;
375        return true;
376    }
377};
378var Validate = {
379    Presence: function(B, C) {
380        var C = C || {};
381        var A = C.failureMessage || "Can't be empty!";
382        if (B === "" || B === null || B === undefined) {
383            Validate.fail(A);
384        }
385        return true;
386    },
387    Custom: function(D, E, owner) {
388
389        var E = E || {};
390        var B = E.against || function() {
391            return true;
392        };
393        var A = E.args || {};
394        var C = E.failureMessage || "Not valid!";
395        if (!B(D, A, owner)) {
396            Validate.fail(C, 1);
397        }
398        return true;
399    },
400    Numericality: function(J, E) {
401        var A = J;
402        var J = Number(J);
403        var E = E || {};
404        var F = ((E.minimum) || (E.minimum == 0)) ? E.minimum : null;
405        var C = ((E.maximum) || (E.maximum == 0)) ? E.maximum : null;
406        var D = ((E.is) || (E.is == 0)) ? E.is : null;
407        var G = E.notANumberMessage || "Must be a number!";
408        var H = E.notAnIntegerMessage || "Must be an integer!";
409        var I = E.wrongNumberMessage || "Must be " + D + "!";
410        var B = E.tooLowMessage || "Must not be less than " + F + "!";
411        var K = E.tooHighMessage || "Must not be more than " + C + "!";
412        if (!isFinite(J)) {
413            Validate.fail(G);
414        }
415        if (E.onlyInteger && (/\.0+$|\.$/.test(String(A)) || J != parseInt(J))) {
416            Validate.fail(H);
417        }
418        switch (true) {
419            case (D !== null):
420                if (J != Number(D)) {
421                    Validate.fail(I);
422                }
423                break;
424            case (F !== null && C !== null):
425                Validate.Numericality(J, {
426                    tooLowMessage: B,
427                    minimum: F
428                });
429                Validate.Numericality(J, {
430                    tooHighMessage: K,
431                    maximum: C
432                });
433                break;
434            case (F !== null):
435                if (J < Number(F)) {
436                    Validate.fail(B);
437                }
438                break;
439            case (C !== null):
440                if (J > Number(C)) {
441                    Validate.fail(K);
442                }
443                break;
444        }
445        return true;
446    },
447    Format: function(C, E) {
448        var C = String(C);
449        var E = E || {};
450        var A = E.failureMessage || "Not valid!";
451        var B = E.pattern || /./;
452        var D = E.negate || false;
453        if (!D && !B.test(C)) {
454            Validate.fail(A);
455        }
456        if (D && B.test(C)) {
457            Validate.fail(A);
458        }
459        return true;
460    },
461    Email: function(B, C) {
462        var C = C || {};
463        var A = C.failureMessage || "Must be a valid email address!";
464        Validate.Format(B, {
465            failureMessage: A,
466            pattern: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
467        });
468        return true;
469    },
470    URL: function(B, C) {
471        var C = C || {};
472        var A = C.failureMessage || "Must be a valid address!";
473        Validate.Format(B, {
474            failureMessage: A,
475            pattern: /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/i
476        });
477        return true;
478    },
479    Length: function(F, G) {
480        var F = String(F);
481        var G = G || {};
482        var E = ((G.minimum) || (G.minimum == 0)) ? G.minimum : null;
483        var H = ((G.maximum) || (G.maximum == 0)) ? G.maximum : null;
484        var C = ((G.is) || (G.is == 0)) ? G.is : null;
485        var A = G.wrongLengthMessage || "Must be " + C + " characters long!";
486        var B = G.tooShortMessage || "Must not be less than " + E + " characters long!";
487        var D = G.tooLongMessage || "Must not be more than " + H + " characters long!";
488        switch (true) {
489            case (C !== null):
490                if (F.length != Number(C)) {
491                    Validate.fail(A);
492                }
493                break;
494            case (E !== null && H !== null):
495                Validate.Length(F, {
496                    tooShortMessage: B,
497                    minimum: E
498                });
499                Validate.Length(F, {
500                    tooLongMessage: D,
501                    maximum: H
502                });
503                break;
504            case (E !== null):
505                if (F.length < Number(E)) {
506                    Validate.fail(B);
507                }
508                break;
509            case (H !== null):
510                if (F.length > Number(H)) {
511                    Validate.fail(D);
512                }
513                break;
514            default:
515                throw new Error("Validate::Length - Length(s) to validate against must be provided!");
516        }
517        return true;
518    },
519    Inclusion: function(H, F) {
520        var F = F || {};
521        var K = F.failureMessage || "Must be included in the list!";
522        var G = (F.caseSensitive === false) ? false : true;
523        if (F.allowNull && H == null) {
524            return true;
525        }
526        if (!F.allowNull && H == null) {
527            Validate.fail(K);
528        }
529        var D = F.within || [];
530        if (!G) {
531            var A = [];
532            for (var C = 0, B = D.length; C < B; ++C) {
533                var I = D[C];
534                if (typeof I == "string") {
535                    I = I.toLowerCase();
536                }
537                A.push(I);
538            }
539            D = A;
540            if (typeof H == "string") {
541                H = H.toLowerCase();
542            }
543        }
544        var J = false;
545        for (var E = 0, B = D.length; E < B; ++E) {
546            if (D[E] == H) {
547                J = true;
548            }
549            if (F.partialMatch) {
550                if (H.indexOf(D[E]) != -1) {
551                    J = true;
552                }
553            }
554        }
555        if ((!F.negate && !J) || (F.negate && J)) {
556            Validate.fail(K);
557        }
558        return true;
559    },
560    Exclusion: function(A, B) {
561        var B = B || {};
562        B.failureMessage = B.failureMessage || "Must not be included in the list!";
563        B.negate = true;
564        Validate.Inclusion(A, B);
565        return true;
566    },
567    Confirmation: function(C, D) {
568        if (!D.match) {
569            throw new Error("Validate::Confirmation - Error validating confirmation: Id of element to match must be provided!");
570        }
571        var D = D || {};
572        var B = D.failureMessage || "Does not match!";
573        var A = D.match.nodeName ? D.match : document.getElementById(D.match);
574        if (!A) {
575            throw new Error("Validate::Confirmation - There is no reference with name of, or element with id of '" + D.match + "'!");
576        }
577        if (C != A.value) {
578            Validate.fail(B);
579        }
580        return true;
581    },
582    Acceptance: function(B, C) {
583        var C = C || {};
584        var A = C.failureMessage || "Must be accepted!";
585        if (!B) {
586            Validate.fail(A);
587        }
588        return true;
589    },
590
591    now: function(A, D, C) {
592        if (!A) {
593            throw new Error("Validate::now - Validation function must be provided!");
594        }
595        var E = true;
596        try {
597            A(D, C || {});
598        } catch (B) {
599            if (B instanceof Validate.Error) {
600                E = false;
601            } else {
602                throw B;
603            }
604        } finally {
605            return E;
606        }
607    },
608    fail: function(A, force_check_empty) {
609        throw new Validate.Error(A, force_check_empty);
610    },
611    Error: function(A, force_check_empty) {
612        this.message = A;
613        this.name = "ValidationError";
614        var force_check_empty = force_check_empty || 0;
615        this.force_check_empty = force_check_empty;
616    }
617};

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.