]> dev.renevier.net Git - syj.git/blob - public/js/forms.js
contact and login js validation: determines required field automatically
[syj.git] / public / js / forms.js
1 /*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
2     and is published under the AGPL license. */
3 Element.addMethods(['input', 'textarea'], {
4     check: function(control, callback, errorMessage) {
5         if (callback.call(control)) {
6             return true;
7         }
8         control.insert({
9             after: new Element("div", {className: 'error'}).update(errorMessage)
10         });
11         return false;
12     }
13 });
14
15 Element.addMethods('form', {
16     focus: function(form) {
17         var tofocus, error;
18
19         tofocus = null;
20         error = form.down('.error');
21         if (error) {
22             tofocus = error.previous('input,textarea');
23         } else {
24             tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])');
25         }
26         if (tofocus) {
27             if (error && (typeof tofocus.highlight === "function")) {
28                 tofocus.highlight('#F08080');
29             }
30             tofocus.focus();
31             tofocus.select();
32         }
33     },
34
35     checkEmptyElements: function(form, errorMessage) {
36         var results = [];
37         form.select('.required').each(function(elt) {
38             var id = elt.getAttribute('for'), control = $(id);
39             if (!control) {
40                 return;
41             }
42             if (!control.check(function() {
43                     return !this.value.strip().empty();
44                 }, errorMessage)) {
45                 results.push(control);
46             }
47         });
48         return results;
49     }
50
51 });