SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array for form elements

    I am trying to use an array to store the names of a form (test) elements (i.e. text boxes) so that I can see if they were left blank.

    the problem I am having is this line of the code

    Code:
    document.test.err[i].value == ""
    firebug my JavaScript debugger is giving me the follow error


    document.test.err has no properties
    [Break on this error] if ( document.test.err[i].value == "" )


    the line should be


    document.test.req1.value == ""

    What am I missing?

    Thanks

    This is the full code
    Code:
            var errmsg = new String();
    	var err = new Array("req1");
    
    	
    	for(i=0;i<err.length;i++) {
    
    		if ( document.test.err[i].value == "" )
    		{
    			errmsg += "Error";
            	
    		}
    
    	}

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    If you give your form an identifier, you can easily get a list of the form elements and check them.

    Code Javascript:
    function validate() {
        var errmsg = '';
        var i, el;
        for (i = 0; i < this.elements.length; i++) {
            el = this.elements[i];
                if (el.type === 'text' && el.value === '') {
                    errmsg = 'Error';
                }
            }
        }
        if (errmsg > '') {
            // do something with the error
            // ...
            // then cancel the submit
            return false;
        }
    }
    document.getElementById('myForm').onsubmit = validate;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    if ( document.test[err[i]]value == "" )


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •