Validating multiple inputs with javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolcat
    New Member
    • Nov 2009
    • 1

    Validating multiple inputs with javascript

    Hi guys, new here and to javascript, I have this form that I am trying to you javascript to validate the multiple input text boxes.

    form as follows
    Code:
    <form action="login.php" method="post" onsubmit="return(checkAll(this))">
    						<label>Username<br /><input id="input[]" type="text" name="username" /></label><br />
    
    						<label>Email<br /><input id="input[]" type="text" name="email" /></label><br />
    
    						<label>Password<br /><input id="input[]" type="password" name="pswd1" /></label><br />
    
    						<label>Password Again<br /><input id="input[] "type="password" name="pswd2" /></label><br />
    			
    						<input type="submit" name="submit" value="Submit" />
    
    						<input type="reset" value="Reset" />
    
    						</form>
    and the javascript
    Code:
    function checkAll(formObj)
    {
    	alert("formObj here");
    	check_obj = document.getElementByID("input[]");
    	alert("formObj here1");
    	for (i=0; i<check_obj.length; i++)
    	{
    			alert("inside formObj here");
    		  if (check_obj[i].value == "")
    		  {
    			alert("Input item on Row #"+(i+1)+" has been left empty. Please make sure you input something into this field.");
    			return false;
    		  }
    	}
    }
    when I use document.getEle mentById("input[]")
    it fails

    when I use document.getEle mentByName("inp ut[]")
    it succeeds,

    However I need the input boxes to have unqiue names so that I can post to php server by names.

    Is document.getEle mentById("input[]") not designed to work with arrays or am I overlooking something?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by lolcat
    Is document.getEle mentById("input[]") not designed to work with arrays or am I overlooking something?
    you overlooked that IDs are meant to be unique (thus your HTML is invalid in this respect). therefore you can say that IDs are not designed to work with arrays.

    PS. and you have quite a lot of typos in your code. the two methods are (case sensitive, of course):
    document.getEle mentById()
    document.getEle mentsByName()

    Comment

    Working...