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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox Validation

Status
Not open for further replies.

smellykat

Programmer
Mar 4, 2002
33
US
I have a table with 2 columns: Vendor Name, Transactions(checkboxes). The amount of checkboxes and their values vary depending on who the vendor is.
For example:
VENDOR NAME TRANSACTIONS
Vendor A name=checkbox value X
name=checkbox value Y
Vendor B name=checkbox value X
name=checkbox value Y
name=checkbox value Z

On clicking next, I need a javascript function that will loop through each vendor in the table and make sure that at least one checkbox is checked for each vendor. So if there are 4 vendors selected into the table by the user, then at least 4 checkboxes should be checked, but more is ok.
Each vendor needs at least one box checked.

Thanks.
 
var blnValorA=false;
for (i=0;i<document.forms['boxes'].length;i++)
{
strValor= document.forms['boxes'].item(i).name;
str=strValor.substr(0,6);
if (str=='vendorA')
{
if (document.forms['boxes'].item(i).checked)
{
blnValorA=true
}
}
}

if (!blnValorA) alert ('You must choose atleast one of the VendorA');
else
//proceed
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Anikin,

Thanks for your reply. I'm a bit confused as to what this line does:
str=strValor.substr(0,6);

can you explain?
 
it's not a 6 but a 7 for the case i gave you.

This function seeks all the fields of the boxes form and if the fieldname starts by vendorA it sets a flag.

that line, just get the first 6 chars of the strings.

string substr(start,lenght)

substr is a function of a string that extracts a substring starting in the start position with length chars, or until the end of the string. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
This code always gives me the alert no matter whether i check a box or not. Heres the code:

function validateDUN( form )
{ var blnValorA=false;


for (i=0;i<form['transactions'].length;i++)
{
strValor= form['transactions'].item(i).name;
str=strValor.substr(0,6);
if (str=='Vendor')
{
if (form['transactions'].item(i).checked)
{
blnValorA=true
}
}
}

if (blnValorA==false) alert ('You must choose atleast one of the Vendor');

else
return true;
return false;

}

Here are my checkboxes:

<form name=&quot;form2&quot; method=&quot;post&quot; action=
<%
Response.Write(&quot;na_payment_method.asp?ID=&quot;);
%> onSubmit=&quot;return validateDUN(this)&quot;>

<td width=&quot;311&quot; align=&quot;left&quot;>
<%
if (rs(&quot;trading_partner&quot;) == &quot;VendorA&quot; || rs(&quot;trading_partner&quot;) == &quot;VendorB|| rs(&quot;trading_partner&quot;) == &quot;VendorC&quot;|| rs(&quot;trading_partner&quot;) == &quot;VendorD&quot;)
{
%>
<input type=&quot;checkbox&quot; name=&quot;transactions&quot; value=&quot;<% =rs(&quot;ID&quot;) %>L1&quot;>Purchase Order, Shipment Notice <br>    and DC Invoice
<br>
<input type=&quot;checkbox&quot; name=&quot;transactions&quot; value=&quot;<% =rs(&quot;ID&quot;) %>L2&quot;>Direct Store Delivery Invoice
<%
if (rs(&quot;trading_partner&quot;) == &quot;VendorD&quot;)
{
%>
<br>
<input type=&quot;checkbox&quot; name=&quot;transactions&quot; value=&quot;<% =rs(&quot;ID&quot;) %>L3&quot;>Sales Information
<%
}
}
if (rs(&quot;trading_partner&quot;) == &quot;VendorE&quot;)
{
%>
<input type=&quot;checkbox&quot; name=&quot;transactions&quot; value=&quot;<% =rs(&quot;ID&quot;) %>L4&quot;>Purchase Order, Shipment Notice, DC<br>    Invoice and Direct Store Delivery Invoice

<input type=&quot;checkbox&quot; name=&quot;transactions&quot; value=&quot;<% =rs(&quot;ID&quot;) %>L5&quot;>Item Price Maintenance

<%
}
//proceed....
</form>
 
for (i=0;i<form.length;i++)
{
strValor= form.item(i).name;
str=strValor.substr(0,6);
if (str=='Vendor')
{
if (form.item(i).checked)
{
blnValorA=true
}
}
}


Try this

Cause you had bad names in the form.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top