~ubuntu-branches/ubuntu/oneiric/libcommons-validator-java/oneiric

« back to all changes in this revision

Viewing changes to src/javascript/org/apache/commons/validator/javascript/validateRequired.js

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 13:35:53 UTC
  • Revision ID: james.westby@ubuntu.com-20041119133553-qtl7whjkjv9q1rbd
Tags: upstream-1.3
ImportĀ upstreamĀ versionĀ 1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
    /*$RCSfile: validateRequired.js,v $ $Revision: 1.13 $ $Date: 2004/03/28 16:53:21 $ */
 
3
    /**
 
4
    *  Check to see if fields must contain a value.
 
5
    * Fields are not checked if they are disabled.
 
6
    * <p>
 
7
    * @param form The form validation is taking place on.
 
8
    */
 
9
 
 
10
    function validateRequired(form) {
 
11
        var isValid = true;
 
12
        var focusField = null;
 
13
        var i = 0;
 
14
        var fields = new Array();
 
15
        var formName = form.getAttributeNode("name");
 
16
 
 
17
        oRequired = eval('new ' + formName.value + '_required()');
 
18
 
 
19
        for (x in oRequired) {
 
20
            var field = form[oRequired[x][0]];
 
21
 
 
22
            if ((field.type == 'hidden' ||
 
23
                field.type == 'text' ||
 
24
                field.type == 'textarea' ||
 
25
                field.type == 'file' ||
 
26
                field.type == 'checkbox' ||
 
27
                field.type == 'select-one' ||
 
28
                field.type == 'password') &&
 
29
                field.disabled == false) {
 
30
 
 
31
                var value = '';
 
32
                // get field's value
 
33
                if (field.type == "select-one") {
 
34
                    var si = field.selectedIndex;
 
35
                    if (si >= 0) {
 
36
                        value = field.options[si].value;
 
37
                    }
 
38
                } else if (field.type == 'checkbox') {
 
39
                    if (field.checked) {
 
40
                        value = field.value;
 
41
                    }
 
42
                } else {
 
43
                    value = field.value;
 
44
                }
 
45
 
 
46
                if (trim(value).length == 0) {
 
47
 
 
48
                    if (i == 0) {
 
49
                        focusField = field;
 
50
                    }
 
51
                    fields[i++] = oRequired[x][1];
 
52
                    isValid = false;
 
53
                }
 
54
            } else if (field.type == "select-multiple") { 
 
55
                var numOptions = field.options.length;
 
56
                lastSelected=-1;
 
57
                for(loop=numOptions-1;loop>=0;loop--) {
 
58
                    if(field.options[loop].selected) {
 
59
                        lastSelected = loop;
 
60
                        value = field.options[loop].value;
 
61
                        break;
 
62
                    }
 
63
                }
 
64
                if(lastSelected < 0 || trim(value).length == 0) {
 
65
                    if(i == 0) {
 
66
                        focusField = field;
 
67
                    }
 
68
                    fields[i++] = oRequired[x][1];
 
69
                    isValid=false;
 
70
                }
 
71
            } else if ((field.length > 0) && (field[0].type == 'radio' || field[0].type == 'checkbox')) {
 
72
                isChecked=-1;
 
73
                for (loop=0;loop < field.length;loop++) {
 
74
                    if (field[loop].checked) {
 
75
                        isChecked=loop;
 
76
                        break; // only one needs to be checked
 
77
                    }
 
78
                }
 
79
                if (isChecked < 0) {
 
80
                    if (i == 0) {
 
81
                        focusField = field[0];
 
82
                    }
 
83
                    fields[i++] = oRequired[x][1];
 
84
                    isValid=false;
 
85
                }
 
86
            }
 
87
        }
 
88
        if (fields.length > 0) {
 
89
           focusField.focus();
 
90
           alert(fields.join('\n'));
 
91
        }
 
92
        return isValid;
 
93
    }
 
94
    
 
95
    // Trim whitespace from left and right sides of s.
 
96
    function trim(s) {
 
97
        return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
 
98
    }
 
 
b'\\ No newline at end of file'