View Full Version : required check box
jayvee
03-09-2008, 08:00 AM
how would i make a check box that is required to be checked.
ex: i accept terms of service
if the user doesn't check it, it says error msg like.. you need to check the terms of service.
thanks in advanced!
coothead
03-09-2008, 11:44 AM
Hi there jayvee,
bearing in mind that form validation really needs to be done server side also, try this...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.highlight{
outline: #f00 dotted 2px;
}
</style>
<!--[if IE]>
<style type="text/css">
.highlight{
border:2px dotted #f00;
}
</style>
<![endif]-->
<script type="text/javascript">
window.onload=function(){
document.forms[0].onsubmit=function(){
return checkIt();
}
}
function checkIt() {
if(document.getElementById('tos').checked==false) {
alert('you need to check the "I accept terms of service" box');
document.getElementById('tos').className='highlight'
return false;
}
else {
document.getElementById('tos').className='';
return true;
}
}
</script>
</head>
<body>
<form action="#">
<div>
<input id="tos" type="checkbox"><label for="tos"> I accept terms of service</label>
<input type="submit">
</div>
</form>
</body>
</html>
coothead
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.