making sure a checkbox is clicked
Moderator: General Moderators
making sure a checkbox is clicked
I am making a form that posts variables to paypal, and I want to make sure that the customer clicks an "I agree to these terms" button before they submit. Is there an easy javascript or something that will make sure that check box is clicked, and if not, make a little box pop up and bitch at them?
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
function acceptTerms() {
if (document.forms['theForm'].elements['theCheckbox'].checked == false) {
alert('Please check the box to continue.');
return false;
} else {
return true;
}
}
</script>
</head>
<body>
<form action="" method="get" name="theForm" onsubmit="return acceptTerms()"/>
<input name="theCheckbox" type="checkbox" value="1" />
<input name="" type="submit" />
</form>
</body>
</html>You could pop up a confirm() box rather than an alert() box. Confirm will return false if the user clicks 'Cancel', and true if the user clicks 'OK'.
Code: Select all
<script>
<html>
<head>
function doubleCheck()
{
var myConfirm = confirm('Are you sure you want to confirm this?');
return(myConfirm);
}
</script>
</head>
<body>
<form method = 'post' action = '$PHP_SELF' onSubmit = "return(doubleCheck());">
...
<input type = "submit">
</form>
</body>
</html>Real programmers don't comment their code. If it was hard to write, it should be hard to understand.