You can remove the line: alert("here"); from the function - I put that there for testing.
Main Topics
Browse All TopicsI'm having prblems with validating a form which uses an array.
I can work with values of normal textboxes etc, but with an array it generates an error - and I need the array as it is dynamically generated in PHP.
Here's the form code:
<FORM name="form1" action="/script.php" onsubmit="return validateForm(form1)">
<input type = "text" name = "length[1]">
<input type = "text" name = "length[2]">
<input type = "text" name = "length[3]">
<input type="submit">
</FORM>
<SCRIPT Language="JavaScript">
function validateForm(theForm)
{ alert(theForm.length[1].va
</SCRIPT>
Do i need to do anything special to read the array element's values?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
i dont think you can name your text boxes that. Give them names like length1, length2, length3
then you can access them like this
for( intIndex = 0; intIndex < form1.elements.length; intIndex++ )
{
if( form1.elements[ intIndex ].name == "length" + intIndex )
{
alert( form1.elements[ intIndex ].value );
}
}
You don't need brackets to define an array in php. If you have textboxes named the same thing, then you can refer to them as an array in php.
This:
<FORM name="form1" action="script.php" onsubmit="return validateForm();" method="post">
<input type ="text" name ="txtlength">
<input type ="text" name ="txtlength">
<input type ="text" name ="txtlength">
<input type="submit">
Can be referred to in PHP as this:
$_POST['txtlength'][0]
$_POST['txtlength'][1]
$_POST['txtlength'][2]
Business Accounts
Answer for Membership
by: cLFlaVAPosted on 2004-09-16 at 06:26:06ID: 12074434
Here is what your function should look like:
alue);
function validateForm() {
alert("here");
for (var i = 0; i < form1.txtlength.length; i++) {
alert(form1.txtlength[i].v
}
}
And here is what your html should look like:
<FORM name="form1" action="script.php" onsubmit="return validateForm();">
<input type ="text" name ="txtlength">
<input type ="text" name ="txtlength">
<input type ="text" name ="txtlength">
<input type="submit">
</FORM>
Note: to actually stop the form from submitting (say, if your validation is invalid - a user enters an invalid value or something), you'll have to return false in the function.