Click to See Complete Forum and Search --> : Validating form
florida
05-13-2003, 02:58 PM
Please advise how I can make sure a check box is checked?
I have check boxes on my form and want to do a data validation to make sure a box is checked. So the form will not go out until one of the boxes has a check in it.
Thanks
Charles
05-13-2003, 03:03 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
<!--
function validate (f) {
for (j=0; j<f.problem.length; j++) {if (f.problem[j].checked) var checked = true;}
if (!checked) {alert ('Please select an option or two?'); return false};
}
// -->
</script>
<form action="" onsubmit="return validate(this)">
<div>
<label><input type="checkbox" name="problem" value="fee">Fee</label><br>
<label><input type="checkbox" name="problem">Fie</label><br>
<label><input type="checkbox" name="problem">Foe</label><br>
<label><input type="checkbox" name="problem">Fum</label><br>
<input type="submit">
</div>
</form>
khalidali63
05-13-2003, 03:05 PM
Check this link out.
It shows how to force a user to select all of the check boxes,could be easily modified for your need. that is,instead of making it check for all of the check boxes make it to look for a particular or at least one be checked.
http://68.145.35.86/skills/javascripts/CheckboxesSelectForceToSelectAll.html
havik
05-13-2003, 04:53 PM
You can edit this code to work as well, just to give you options
http://javascript.internet.com/forms/limit-boxes.html
Havik
florida
05-14-2003, 07:04 AM
thanks!
Here is how it now works for me:
for (j=0; j<f.problem.length; j++)
{
if (f.problem[j].checked)
var checked = true;
}
if (!checked)
{
alert ('Please select an option or two?');
return false
};
}
My question:
For brackets why cant I do this:
for (j=0; j<f.problem.length; j++)
{
if (f.problem[j].checked)
{ //ADDED EXTRA BRACKET HERE
var checked = true;
}
if (!checked)
{
alert ('Please select an option or two?');
return false
};
} //ADDED EXTRA BRACKET HERE
}
When I run it like this (with my bracket modifications) it doesnt work the correct way compared to the first way where I didnt touch the brackets. I thought the brackets I put in would make it easier for me to read and it was based on conditions but i was wrong. Please explain why i CANT add brackets the way I tried??
Padrill
05-14-2003, 09:48 AM
First of all they both have one extra closing bracket - which is syntactically wrong. In the first script the last bracket is ignored (I pressume). The first script executes a loop to se if any of the checkboxes are checked and if at least one is then it assigns true to a variable. Next, it checks the variable and if it is not true it displays an error message.
Your script is equivalent to:
for (j=0; j<f.problem.length; j++)
{
if (f.problem[j].checked) var checked = true;
if (!checked)
{
alert ('Please select an option or two?');
return false
}
}
Do you see why now?
florida
05-14-2003, 12:48 PM
Thanks, I understand what your saying but for my visual clarity I thought I could do this:
for (j=0; j<f.problem.length; j++)
{
if (f.problem[j].checked)
{
var checked = true;
}
But I guess that is not possible with this loop.