Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable name problem 2

Status
Not open for further replies.

yoshismokey

Programmer
Sep 17, 2005
25
US
I am having a problem accessing my cold fusion variables in a javascript function to validate the form fields.
Here is the cold fusion section where I am populating the form fields:

<form name="addfamily" action="AddUprForm.cfm" method="post" onSubmit="return CheckForm(this)">
<!--- Loop through the query and pull out the Product Families. --->
<cfoutput query="getinfo">
<tr>
<td align="center"><input type="Text" name="qty_#getinfo.Prod_Family_ID#" size="3" maxlength="5"></td>
<td width="266" valign="top" align="left"> <p> <font size="3" face="Arial, Helvetica, sans-serif" color="##C3046D"><strong>#getinfo.Prod_Family_Name#</strong></font></td>
<td align="center"><input type="text" name="discount_#getinfo.Prod_Family_ID#" size="3" maxlength="5">
%</td>
</tr>
</cfoutput>



And here is the javascript:

<script language="JavaScript">
<!--
//this function checks that if a quantity is entered,
//a discount is entered and vice versa

function CheckForm(form) {
var i, qty, discount;
var numproducts = 17;

for (i=1; i<numproducts; i++){
qty = "qty_" + i;
discount = "discount_" + i;

if (form.qty.value != "" && form.discount.value) == ""){
alert("You must enter a discount for each quantity entered");
return false;
}
else {
if (form.qty.value == "" && form.qty.value != ""){
alert("You must enter a quantity for each discount entered");
return false;
}
else {
return true;
}
}

}

}
</script>

The variables for quantity are qty_1 through qty_16; for discount - discount_1 through discount_16.
When I try to access the value in the fields by calling them qty_i and discount_i with i being the numbers 1 through 16, it doesn't work. What am I missing?
Thanks in advance,
Robin
 
change
form.qty.value

to
form.elements[qty].value

likewise for discount

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Thanks so much for replying. I tried changing to form.elements[qty].value, but still get a javascript error - "expression expected". Do I need something else?
- Robin
 
As well as the chnage Jeff gave you, you should fix the syntax problem in your first "if" statement. You have one too many closing brackets. This:

Code:
if (form.elements[qty].value != "" && form.elements[discount].value[b])[/b] == "") {


should be:

Code:
if (form.elements[qty].value != "" && form.elements[discount].value == "") {

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
That's it!!!!!
Thanks to both of you - it works like a charm!
Robin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top