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

validating a dynamic check box

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hi all!

Guys i have a problem how can i validate dynamically created chekboxes ,I tried in the following way,

But it alerts only when all the checkboxes not checked ,and if you check other than the first checkbox it still gives a alert box please check the check box ,

I need if the user selects atleast one checkbox it has to submit

How i can over come this please help me

ThanX:)


<html>
<head>
<title>Checking The Checkbox</title>
<script>
function check(field)
{
var count=0;
for(var x=0; x < field.length; ++x)
{


if(field.elements[x].type == &quot;checkbox&quot;)
{
if(!(field.elements[x].checked))
{
alert(&quot;Please select a check box&quot;);
return false;
}
else
{
document.f1.submit();
return true;
}


}
}


}
</script>
</head>
<body>
<form name=f1>
One <input type=checkbox name=one1 value=&quot;one&quot;><br>
Two <input type=checkbox name=one2 value=&quot;two&quot;><br>
Three <input type=checkbox name=one3 value=&quot;three&quot;><br>
Four <input type=checkbox name=one4 value=&quot;Four&quot;><br>
Five <input type=checkbox name=one5 value=&quot;five&quot;><br>
<input type=button value=submit onClick=check(this.form);>
</form>
</body>
</html>


 
Hi thendal,

Try this.. =)

<html>
<head>
<title>Checking The Checkbox</title>
<script>
function check(field)
{
var count=0;
for(var x=0; x<field.length; x++)
{
if(field.elements[x].type == &quot;checkbox&quot;)
{
if(field.elements[x].checked)
{
document.f1.submit();
return true;
}
}
}
alert(&quot;Please select a check box&quot;);
return false;
}
</script>
</head>
<body>
<form name=f1>
One <input type=checkbox name=one1 value=&quot;one&quot;><br>
Two <input type=checkbox name=one2 value=&quot;two&quot;><br>
Three <input type=checkbox name=one3 value=&quot;three&quot;><br>
Four <input type=checkbox name=one4 value=&quot;Four&quot;><br>
Five <input type=checkbox name=one5 value=&quot;five&quot;><br>
<input type=button value=submit onClick=check(this.form);>
</form>
</body>
</html>

hope this helps, Chiu Chan
cchan@gefmus.com
 
Thanks buddy ,

I think i need to upgrade my processor(brain)

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top