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

dynamic forms validation

Status
Not open for further replies.

irate

Programmer
Joined
Jan 29, 2001
Messages
92
Location
GB
I have a form with a drop down and a number of checkboxes.

The drop down simply has numbers 1 to 5 in it.

The checkboxes are created by asp and there can be any number of them depending on the database. The checkboxes are named 'item' & the number of check box so for example the first checkbox would be called item1 and the nth checkbox would be called itemn.

Here is an example of the form:

<form action=&quot;newhomepageadminupdate.asp&quot; method=&quot;post&quot;>
<SELECT NAME=&quot;numoflate&quot;>
<OPTION VALUE=&quot;1&quot;>1</OPTION>
<OPTION VALUE=&quot;2&quot;>2</OPTION>
<OPTION VALUE=&quot;3&quot;>3</OPTION>
<OPTION VALUE=&quot;4&quot;>4</OPTION>
<OPTION VALUE=&quot;5&quot;>5</OPTION>
</SELECT>
<% response.write(&quot;<input type='checkbox' name='item&quot;&itemnumber&&quot;' value='&quot;&getitemsRS(&quot;ID&quot;)&&quot;'>&quot;) %>
<% response.write(&quot;<input type='hidden' name='itemnumber' value='&quot;&itemnumber&&quot;'>&quot;) %>
<input type=&quot;submit&quot; value=&quot;make changes&quot; onclick=&quot;return Validate(this.form)&quot;>
</form>

The asp simply adds in the numbers from the database (obtained somewhere else in the script)

I have a javascript function that must validate the form when someone submits it. The only validation criteria is that the number of checkboxes checked by the user must be the same amount they selected in the dropdown list.

i currently have something similar to this:

<script>
<!--//
function Validate(theForm)
{
var checknum = theForm.itemnumber.value;
var howmany = theForm.numoflate.value;
var itemchecked = 0;
for (i = 0; i < checknum; i++)
{
if (theForm.
item+i.checked)
itemchecked += 1;
}
if (itemchecked != howmany)
{
alert(&quot;You must check the same amout of boxes as you have selected in the dropdown menu!&quot;);
return false;
}
else
return true;
}
//-->
</script>

As you can see from the highlighted red text i am having difficulty finding out if the dynamicaly named checkboxes are checked by the user.

Can someone tell me how to use two variables to get a name of a check box or offer a better solution to this problem?

The validation must be performed client side when the form is submitted

thanks in advance
 
For each form in an HTML document you have an array(or collection if you prefer) of its elements indexed from 0.
The elements can be input, slecet, checkbox, etc.
If the code is:
<form name=&quot;tForm&quot;>
<input type=&quot;text&quot; name=&quot;i1&quot;>
<input type=&quot;checkbox&quot; name=&quot;c1&quot; value=&quot;CPP&quot;>
</form>

You can acces the c1 element either as:
document.tForm.c1.value- will return the value of the element
document.tForm.elements[1].type - will return the type of the element with index 1 in the elements collection - our c1
document.tForm.elements.length - return number of all form's elements
ind = 1
document.tForm.elements[ind].checked - true/false

You can figure out which index willbe in your case the start or you can loop from 0 to document.tForm.elements.length and check if the type of element is checkbox, then if it is checked, and so on.

Wish you success!


 
Thanks, i managed to figure it out with a little debugging.
Just got to make sure i dont forget there are a certain amount of elements before and after the checkboxes...

<script>
<!--//
function Validate(theForm)
{
var checknum = theForm.elements.length - 4;
var howmany = theForm.numoflate.value;
var itemchecked = 0;
for (i = 1; i < checknum; i++)
{
//alert(theForm.elements.type);
if (theForm.elements.checked)
{
itemchecked += 1;
//alert(&quot;&quot;+checknum+&quot; &quot;+howmany+&quot; &quot;+itemchecked);
}
// alert(itemchecked);
}
if (itemchecked != howmany)
{
alert(&quot;You must check the same amout of boxes as you have selected in the dropdown menu!&quot;);
return false;
}
else
//alert(&quot;all good&quot;)
return true;
}
//-->
</script>
 
Replace your if statement
if (theForm.item+i.checked)

with these two lines:
[tt]
obj = eval(&quot;document.theForm.item&quot; + i + &quot;.checked&quot;);
if (obj)
[/tt]
Check this and tell the result.

good luck
 
Yep, irate,that code looks like it has to be. Only TGML in the tek-tips forums replaced your
Code:
[i]
with italics.
Check also starway solution :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top