Click to See Complete Forum and Search --> : Compare two Dynamic form elements...Javascript


Arc
06-05-2007, 10:44 PM
Hey guys. I am dynamicaly creating a form with a select box and a text box.
The form will have 1-however many rows. The select box and text box are arrays.

On the form submit I call a javascript function Validate to compare the two fields on each row. For some reason I am getting an error that says "value is null or not an object".

Here is the javascript I have

function Validate() {
var selNewStatus = document.myForm.selNewStatus;
var txtRO = document.myForm.txtRO;
var err = "Please Make These Corrections....";

for (i = 0; i < document.myForm.selNewStatus.length; i++) {
if(selNewStatus[i].value == "Approved" && txtRO[i].value == ""){

err+="\nYou must enter an RO when marking an estimate approved. Line Number: "+(i+1);

}//end if
}//end for
if(err != "Please Make These Corrections...."){
alert (err);
return false;
}//end if
}//end Validate


the two form elemets are named like so


<select name="selNewStatus[]" id="selNewStatus">
<option value="None" selected>No Change</option>
<option value="Approved">Approved</option>
<option value="Lost">Lost</option>
</select>

<input name="txtRO[]" type="text" class="textRow" id="txtRO" value="<?php echo $ro; ?>">


Any idea why this is not working? It is driving me nuts.

Thanks for the help!

Weedpacket
06-06-2007, 04:09 AM
You haven't got a form field named "selNewStatus" nor one named "txtRO". You have form fields named "selNewStatus[]" and "txtRO[]".

http://nz2.php.net/manual/en/faq.html.php#faq.html.select-multiple

Arc
06-06-2007, 12:37 PM
Thanks a lot for the help Weed, I think I get what you are saying, however I am still having no luck.

I changed the code to where it is directly calling the form element by its array number. The error changed to "document.myForm.txtRO[i].value is null or not an object" Hrmm, I don't get it.

I am merely creating an array of text boxes then trying to loop thru them, why is it so hard? heh.


function Validate() {
var err = "Please Make These Corrections....";

for (i = 0; i < document.myForm.selNewStatus.length; i++) {
if(document.myForm.selNewStatus[i].value == "Approved" && document.myForm.txtRO[i].value == ""){

err+="\nYou must enter an RO when marking an estimate approved. Line Number: "+(i+1);

}//end if
}//end for
if(err != "Please Make These Corrections...."){
alert (err);
return false;
}//end if
}//end Validate


But that doesn't work either....

Arc
06-06-2007, 12:48 PM
Hrmm ok, I figured one thing out at least. The code works fine when there is more than one row, however if there is only one row I get the error mentioned above. Why is that?