How to validate a checkbox

liunx

Guest
Hi Guys,<br />
<br />
I'm looking for the code to validate a checkbox in a form when the user submits it. Dreamweaver doesn't seem to have that capability, so... I'm looking for some magnanimous sole here to help.<br />
<br />
<br />
TIA<br />
Aronya1<!--content-->I assume you mean checked or not:<br />
if(document.formname.checkboxname.checked==true) {<br />
alert("checked");<br />
}<br />
else {<br />
alert("not checked");<br />
}<!--content-->Hi Fang, <br />
<br />
Thanks for helping, but where do I place your code?<!--content-->Between the <head> tags:<br />
<br />
<head><br />
.<br />
.<br />
.<br />
<script type="text/javascript"><br />
<!--<br />
function Verify() {<br />
if(document.formname.checkboxname.checked==true) { <br />
alert("checked"); <br />
} <br />
else { <br />
alert("not checked"); <br />
}<br />
}<br />
//--><br />
</script><br />
</head><!--content-->It's not working.<br />
<br />
Does it matter that I have a typical form validation happening at the same time? (that's working fine)<!--content--><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"<br />
"http://www.w3.org/TR/html4/strict.dtd"><br />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><br />
<meta name="Content-Script-Type" content="text/javascript"><br />
<title>Example</title><br />
<br />
<style type="text/css"><br />
<!-- <br />
label {display:block; margin:1em 0em}<br />
--><br />
</style><br />
<br />
<script type="text/javascript"><br />
<!--<br />
function validate(f) {<br />
alert(f.foo.checked ? 'checked' : 'un-checked');<br />
return false;<br />
}<br />
// --><br />
</script><br />
<br />
<form action="" onsubmit="return validate(this)"><br />
<div><br />
<label><input type="checkbox" name="foo">Foo</label><br />
<button type="submit">Submit</button><br />
</div><br />
</form><!--content-->Hi Charles,<br />
<br />
Thanks for wading in on this one. Your script works on its own (I haven't had time to insert it into my form yet), but it gives an alert whether the box is checked or not. I've tinkered with it, but can't get it to alert only if the box is unchecked.<br />
<br />
Sorry to keep coming back at you guys like this, but I don't know what I'm doing with javascript.<br />
<br />
Thanks again,<br />
Aronya1<!--content-->I tried this and it works when the checkbox is unchecked.<br />
Originally posted by Charles <br />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"<br />
"http://www.w3.org/TR/html4/strict.dtd"><br />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><br />
<meta name="Content-Script-Type" content="text/javascript"><br />
<title>Example</title><br />
<br />
<style type="text/css"><br />
<!-- <br />
label {display:block; margin:1em 0em}<br />
--><br />
</style><br />
<br />
<script type="text/javascript"><br />
<!--<br />
function validate(f) {<br />
alert(f.foo.checked ? 'checked' : 'un-checked');<br />
return false;<br />
}<br />
// --><br />
</script><br />
<br />
<form action="" onsubmit="return validate(this)"><br />
<div><br />
<label><input type="checkbox" name="foo">Foo</label><br />
<button type="submit">Submit</button><br />
</div><br />
</form><!--content-->Yes, but it gives an alert when the checkbox IS checked, too.<br />
<br />
I need an alert when the box is not checked. Period. Giving an alert for both states doesn't validate anything.<!--content--><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"<br />
"http://www.w3.org/TR/html4/strict.dtd"><br />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><br />
<meta name="Content-Script-Type" content="text/javascript"><br />
<title>Example</title><br />
<br />
<style type="text/css"><br />
<!-- <br />
label {display:block; margin:1em 0em}<br />
--><br />
</style><br />
<br />
<script type="text/javascript"><br />
<!--<br />
function validate(f) {if (!f.foo.checked) {alert('Good God man! Check the box.'); f.foo.focus(); return false}}<br />
// --><br />
</script><br />
<br />
<form action="" onsubmit="return validate(this)"><br />
<div><br />
<label><input type="checkbox" name="foo">Foo</label><br />
<button type="submit">Submit</button><br />
</div><br />
</form><!--content-->Charles, you're beautiful.<br />
<br />
Thank you very much.<!--content-->
 
Back
Top