...

View Full Version : Form.elements validation problem



ptaylor
01-29-2006, 01:54 AM
Hi,
Having a problem getting a simple JavaScript function to work. Trying to do a simple form validation using onSubmit within the <form> tag, but I am writing out the form elements using PHP, and some of them are repeated 3 times, with the form names set to:

<input type="text" name="edu[]" size="24" />

it is for an online resume builder, and I need to give people the option of entering up to three entries for their education. When the form is submitted, the form elements that have the same name must include the "[]" in the name field so that the PHP code knows to expect an array of data.

I need to validate that one field, to ensure applicants have entered at least one entry for their education, and I am not sure how to do it. This is the function I have now:

function check(){
if (document.form1.edu[].value==""){
alert("Please enter at least one entry for your education ");
return false;
}
}

Of course this results in an error when the form is submitted...as the syntax is not correct. Can anyone help me with this? Just don't know how to get Javascript to deal with the textbox name "edu[]"

I have tried this:
function check(){
if (document.form1.elements['edu[]'].value==""){
alert("Please enter at least one entry for your education ");
return false;
}
}
but it didn't work...

Many thanks,
Paul

arnyinc
01-29-2006, 01:57 AM
Just to clarify, do you have three text boxes named edu[]?

Like this?

<form>
<input type="text" name="edu[]" size="24" />
<input type="text" name="edu[]" size="24" />
<input type="text" name="edu[]" size="24" />
</form>

ptaylor
01-29-2006, 02:00 AM
three text boxes are written out dynamically via PHP, and everything works great on the server side, with the PHP parsing the array and extracting the values. I am just stuck on how to perform the client-side validation to ensure the user fills in at least one of the textboxes.

Thanks,
Paul

glenngv
01-30-2006, 07:12 AM
I have tried this:
function check(){
if (document.form1.elements['edu[]'].value==""){
alert("Please enter at least one entry for your education ");
return false;
}
}
but it didn't work...

Many thanks,
PaulThat is almost correct. But you have multiple elements with that same name, so you must access them the way you do in array.

if (document.form1.elements['edu[]'][0].value==""){ //first field
Here's the code:

function check(f){
var educs = f.elements['edu[]'];
if (educs.length){ //multiple fields
for (var i=0; i<educs.length; i++){
if (educs[i].value==""){
alert("Please enter at least one entry for your education ");
return false;
}
}
}
else{ //only 1 field
if (educs.value==""){
alert("Please enter your education ");
return false;
}
}
return true;
}
...
<form onsubmit="return check(this)">
...
<input type="submit" value="Submit" />
</form>
I added a check if the fields are multiple or not to accommodate if only 1 field is dynamically generated.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum