SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Location
    Santa Fe, NM
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help w/object and array syntax

    Hi,

    I'm just getting started with JavaScript and am wondering how to substitute a field name stored in an array into a loop that does basic field validation.

    Code:
    function check () {
    	var checkfields = new Array ("filedname1", "fieldname2");
    	for (var i=0; i<checkfields.length; i++) {
    		// How do I substitute the current value of checkfield here?
    		if (document.forms.submission.checkfields[i].value == "") { 
    			alert ("please enter a value for " + checkfields[i]);
    			return false;
    		} else {
    			return true;
    		}
    	}
    }
    Thanks,

    Kevin
    Last edited by kevinpd; Feb 25, 2003 at 22:00.

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this should work:

    eval(document.forms.submission.checkfields[i]).value

    Because the array contains a string of the name and eval() will give you the actual object. If that doesnt work try:

    document.forms.submission.eval(checkfields[i]).value

    I'm too lazy to test it.

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Location
    Santa Fe, NM
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vasudevelopa
    eval(document.forms.submission.checkfields[i]).value

    Because the array contains a string of the name and eval() will give you the actual object. If that doesnt work try:

    document.forms.submission.eval(checkfields[i]).value
    The first form of eval didn't work while the second form does substitue the first item in my array into the object name but for some reason the script doesn't continue on to the next item in the array.

    Any other ideas would be much appreciated.

    Kevin
    Code:
    function check () {
    	var checkfields = new Array ("firstname", "lastname");
    	for (var i=0; i<checkfields.length; i++) {
    		// How do I substitute the current value of checkfield here?
    		if (document.forms.submission.eval(checkfields[i]).value == "" ) { 
    			alert ("please enter a " + checkfields[i]);
    			return false;
    		} else {
    			return true;
    		}
    	}
    }

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Best way...
    Code:
    function check()
    {
    	var checkfield, f = document.forms.submission;
    	var checkfields = new Array("filedname1", "fieldname2" );	
     
    	for (var i = 0; ( checkfield = f.elements[checkfields[i]] ); i++)
    	{
    		if ( checkfield.value == "" )
    		{ 
    			alert ("please enter a value for " + checkfield.name );
    			return false;
    		}
    		else
    		{
    			return true;
    		}
    	}
    }
    Last edited by beetle; Feb 26, 2003 at 14:23.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Location
    Santa Fe, NM
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your response, beetle. This little function is still not working the way I expect. It checks for fieldname1 but doesn't appear to move along to the second item in the array and validate fieldname2. Thanks for the insight on other ways to construct the object reference. Out of curiosity, what does the elements property do? I'm still trying to figure out how to use an array of required fields with a validation function.

    Kevin
    Code:
    <html>
    <head>
    	<title>Test</title>
    	<script type="text/javascript" language="Javascript" charset="iso-8859-1">
    <!--
    function check()
    {
    	var checkfield, f = document.forms.submission;
    	var checkfields = new Array("fieldname1", "fieldname2" );	
     
    	for (var i = 0; ( checkfield = f.elements[checkfields[i]] ); i++)
    	{
    		if ( checkfield.value == "" )
    		{ 
    			alert ("please enter a value for " + checkfield.name );
    			return false;
    		}
    		else
    		{
    			return true;
    		}
    	}
    }//-->
    	</script>
    </head>
    
    <body>
    <form action="somefile.php" id="submission" name="submission" onSubmit="return check();">
    Fieldname1 <input type="text" size="25" name="fieldname1"><br>
    Filedname2 <input type="text" size="25" name="fieldname2"><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to return true after checking ALL fields, not after each successful field.
    Code:
    function check()
    {
    	var checkfield, f = document.forms.submission;
    	var checkfields = new Array("fieldname1", "fieldname2" );
    Code:
    	for (var i = 0; ( checkfield = f.elements[checkfields[i]] ); i++)
    	{
    		if ( checkfield.value == "" )
    		{
    			alert ("please enter a value for " + checkfield.name );
    			return false;
    		}
    	}
    	return true;
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Location
    Santa Fe, NM
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    You need to return true after checking ALL fields, not after each successful field.
    How funny. I just realized that but you beat me to the reply

    Thanks for your great help. The learning continues.

    Kevin

    P.S. Can someone point me to some docs on the elements property mentioned in the code above?
    Last edited by kevinpd; Feb 26, 2003 at 15:47.

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You bet. And yes, it is a property, but more commonly called a collection
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •