CodyJava
11-16-2012, 01:31 AM
I'm trying to protect against empty check boxes. The code works when their empty but also when they aren't so I guess it's not truly working. Also I know there has to be an easier way to check to see if their empty.
Javascript:
if(document.form[0].interest1.checked == false &&
document.form[0].interest2.checked == false &&
document.form[0].interest3.checked == false &&
document.form[0].interest4.checked == false &&
document.form[0].interest5.checked == false){
window.alert("You did not select an interest");
return false;
}else{
return true;
}
html:
<input type="checkbox" name="interest1" value="entertainment">Entertainment
<br>
<input type="checkbox" name="interest2" value="business">Business
<br>
<input type="checkbox" name="interest3" value="music">Music
<br>
<input type="checkbox" name="interest4" value="shopping">Shopping
<br>
<input type="checkbox" name="interest5" value="travel">Travel
Full code(in case needed):
<!DOCTYPE HTML>
<html>
<head>
<title>Chapter 11</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function confirmsubmit(){
if(document.forms[0].visitor_name.value == "" || document.forms[0].visitor_name.value == "Enter your name"){
window.alert("You did not enter your name");
return false;
}
if(document.forms[0].email.value == "" ||
document.forms[0].email.value == "Enter your e-mail address"){
window.alert("You did not enter your email address");
return false;
}
if(document.forms[0].pass1.value != document.forms[0].pass2.value){
window.alert("Your passwords do not match");
}
if(document.forms[0].pass1.value == "" || document.forms[0].pass2.value == ""){
window.alert("One of your passwords are blank");
}
if(document.forms[0].question.value == "blank"){
window.alert("You did not select a security question");
}
if(document.forms[0].ans.value == ""){
window.alert("Your security question answer is blank");
}
var emailvis = false;
for(var i=0;i<2;++i){
if(document.forms[0].offer[i].checked == true){
emailvis = true;
break;
}
if(emailvis == false){
window.alert("You must select a interest");
return false;
}
}
if(document.form[0].interest1.checked == false &&
document.form[0].interest2.checked == false &&
document.form[0].interest3.checked == false &&
document.form[0].interest4.checked == false &&
document.form[0].interest5.checked == false){
window.alert("You did not select an interest");
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<h1>Web Site Registration Form</h1>
<form action="" method="post" onsubmit="return confirmsubmit()">
<h2>Personal Information</h2>
<p>Name
<br>
<br>
<input type="text" value="Enter your name" name="visitor_name"
onclick="if (this.value == 'Enter your name') this.value = '';"
size="50">
<br>
<br>
E-mail address
<br>
<br>
<input type="text" value="Enter your e-mail address" name="email"
onclick="if (this.value == 'Enter your e-mail address') this.value = '';"
size="50">
<br>
</p>
<h2>Security Information</h2>
<p>Enter a password that you can use to manage your subscription online
<br>
<br>
<input type="text" name="pass1" value="" size="50">
<!-- onblur??? -->
<br>
<br>
Type the password again to confirm it
<br>
<br>
<input type="text" name="pass2" value="" size="50">
<br>
<br>
Security question
<br>
<br>
<select name="question">
<option value="blank">[Select a security question]</option>
<option value="mother">What is your mother's maiden name?</option>
<option value="pet">What is the name of pet?</option>
<option value="color">What is your favorite color?</option>
</select>
<br>
<br>
Your answer
<br>
<br>
<input type="text" name="ans" value="" size="50">
<br>
</p>
<h2>Preferences</h2>
<p>Send special offers to my e-mail address
<input type="radio" name="offer" value="Yes">Yes
<input type="radio" name="offer" value="No">No
<br>
<br>
Select areas of interest(select at least one)
<br>
<br>
<input type="checkbox" name="interest1" value="entertainment">Entertainment
<br>
<input type="checkbox" name="interest2" value="business">Business
<br>
<input type="checkbox" name="interest3" value="music">Music
<br>
<input type="checkbox" name="interest4" value="shopping">Shopping
<br>
<input type="checkbox" name="interest5" value="travel">Travel
<br>
<br>
</p>
<p><input type="submit" /></p>
</form>
</body>
</html>
Thanks any help is appreciated.
Javascript:
if(document.form[0].interest1.checked == false &&
document.form[0].interest2.checked == false &&
document.form[0].interest3.checked == false &&
document.form[0].interest4.checked == false &&
document.form[0].interest5.checked == false){
window.alert("You did not select an interest");
return false;
}else{
return true;
}
html:
<input type="checkbox" name="interest1" value="entertainment">Entertainment
<br>
<input type="checkbox" name="interest2" value="business">Business
<br>
<input type="checkbox" name="interest3" value="music">Music
<br>
<input type="checkbox" name="interest4" value="shopping">Shopping
<br>
<input type="checkbox" name="interest5" value="travel">Travel
Full code(in case needed):
<!DOCTYPE HTML>
<html>
<head>
<title>Chapter 11</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function confirmsubmit(){
if(document.forms[0].visitor_name.value == "" || document.forms[0].visitor_name.value == "Enter your name"){
window.alert("You did not enter your name");
return false;
}
if(document.forms[0].email.value == "" ||
document.forms[0].email.value == "Enter your e-mail address"){
window.alert("You did not enter your email address");
return false;
}
if(document.forms[0].pass1.value != document.forms[0].pass2.value){
window.alert("Your passwords do not match");
}
if(document.forms[0].pass1.value == "" || document.forms[0].pass2.value == ""){
window.alert("One of your passwords are blank");
}
if(document.forms[0].question.value == "blank"){
window.alert("You did not select a security question");
}
if(document.forms[0].ans.value == ""){
window.alert("Your security question answer is blank");
}
var emailvis = false;
for(var i=0;i<2;++i){
if(document.forms[0].offer[i].checked == true){
emailvis = true;
break;
}
if(emailvis == false){
window.alert("You must select a interest");
return false;
}
}
if(document.form[0].interest1.checked == false &&
document.form[0].interest2.checked == false &&
document.form[0].interest3.checked == false &&
document.form[0].interest4.checked == false &&
document.form[0].interest5.checked == false){
window.alert("You did not select an interest");
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<h1>Web Site Registration Form</h1>
<form action="" method="post" onsubmit="return confirmsubmit()">
<h2>Personal Information</h2>
<p>Name
<br>
<br>
<input type="text" value="Enter your name" name="visitor_name"
onclick="if (this.value == 'Enter your name') this.value = '';"
size="50">
<br>
<br>
E-mail address
<br>
<br>
<input type="text" value="Enter your e-mail address" name="email"
onclick="if (this.value == 'Enter your e-mail address') this.value = '';"
size="50">
<br>
</p>
<h2>Security Information</h2>
<p>Enter a password that you can use to manage your subscription online
<br>
<br>
<input type="text" name="pass1" value="" size="50">
<!-- onblur??? -->
<br>
<br>
Type the password again to confirm it
<br>
<br>
<input type="text" name="pass2" value="" size="50">
<br>
<br>
Security question
<br>
<br>
<select name="question">
<option value="blank">[Select a security question]</option>
<option value="mother">What is your mother's maiden name?</option>
<option value="pet">What is the name of pet?</option>
<option value="color">What is your favorite color?</option>
</select>
<br>
<br>
Your answer
<br>
<br>
<input type="text" name="ans" value="" size="50">
<br>
</p>
<h2>Preferences</h2>
<p>Send special offers to my e-mail address
<input type="radio" name="offer" value="Yes">Yes
<input type="radio" name="offer" value="No">No
<br>
<br>
Select areas of interest(select at least one)
<br>
<br>
<input type="checkbox" name="interest1" value="entertainment">Entertainment
<br>
<input type="checkbox" name="interest2" value="business">Business
<br>
<input type="checkbox" name="interest3" value="music">Music
<br>
<input type="checkbox" name="interest4" value="shopping">Shopping
<br>
<input type="checkbox" name="interest5" value="travel">Travel
<br>
<br>
</p>
<p><input type="submit" /></p>
</form>
</body>
</html>
Thanks any help is appreciated.