Form validation?
- Started
- Last post
- 12 Responses
- boobs
I am having such a hard time getting my head around this. I've looked around on the internets, and I just can't seem to understand how to do this. How do I set it up so that if a checkbox on a form is not checked, the form can't be submitted?
- boobs0
They have to check the checkbox for the form to be submitted.
- boobs0
That doesn't help at all!
No squeeze for you!
- fugged0
not sure whatcha need then?
- fugged0
submit handler function should return false to prevent the form from submitting.
so in your handler function you need to test and see if the checkbox is checked. if so, return true - (and the for will submit) otherwise return false (and the form will be prevented from submitting)
- ETM0
So I'll assume you mean in javascript since you didn;t say.
if (frm.insertboxname.checked == false) {
alert ('** Error message **');
return false;
}
- fugged0
I gotcha boobs. check it again.:
http://gist.github.com/298594
- boobs0
Well, fugged, I put that into my page, and it doesn't seem to work. Even if the box is not checked, the form still submits, and the information goes into the database.
- fugged0
make sure you don't have any other javascript errors going on. if you have a url, I can help you debug what's going on.
- ayport0
Can always do it server side:
HTML:
<form action="formProcessor.php" method="post">
<input type="checkbox" value="yes" name="checkbox">
<input type="submit">
</form>PHP (asp or asp.net works the same):
<?
if ($_POST['checkbox'] == "yes")
{
// process rest of form
}
else
{
echo "please check the box";
}
?>
- boobs0
fugged got me through it! Big squeeze for fugged!