Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-16-2012, 01:31 AM   PM User | #1
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
Need help using if statement to check checkboxes

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:
Code:
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:
Code:
<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):
Code:
<!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.
CodyJava is offline   Reply With Quote
Old 11-16-2012, 02:02 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
First of all, why do you keep repeating document.forms[0] all over the place?

Code:
function confirmsubmit()
{
    var f = document.forms[0];
    if ( f.visitor_name.value == "" || f.visitor_name.value == "Enter your name")
    {
        window.alert("You did not enter your name");
        return false;
    }
    ...
    ... similarly throughout ...
    ...
    // last step:
    for ( var cb = 1; cb <= 5; ++cb )
    {
        if ( f["interest"+cb].checked ) return true;
    }
    alert("You must check at least one interest";
    return false;
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
CodyJava (11-16-2012)
Old 11-16-2012, 07:20 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
First of all, why do you keep repeating document.forms[0] all over the place?
He ignored the same advice that I gave him in another thread.

He has document.form[0] in several places.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-16-2012 at 07:22 AM..
Philip M is offline   Reply With Quote
Old 11-16-2012, 02:58 PM   PM User | #4
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
I had a template from something else so I copied and pasted a lot of it and when I started doing new code I just kept doing it I'll use a variable next time thanks for all the help.
CodyJava is offline   Reply With Quote
Old 11-16-2012, 09:02 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Philip M View Post
He ignored the same advice that I gave him in another thread.

He has document.form[0] in several places.
DOH on me! OF COURSE!

The whole reason his code didn't work was because he used document.form[0] instead of document.forms[0]!!!

Good eyes, Philip!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-16-2012, 09:54 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Using document.forms[0] ties the JavaScript directly to the HTML so that adding another form to the page (such as if you decide to add a search box into the header of each page) will instantly break your JavaScript.

Using document.getElementsByTagName('visitor_name')[0].form would be a better alternative because it would at least mean that the JavaScript isn't affected unless another form with that same field name in it is added.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-16-2012, 10:15 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yes, but why not simply insist that every <form> have an ID? ID's *MUST* be unique, so there's not even the possibility of a duplicated field name.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-17-2012, 07:35 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
DOH on me! OF COURSE!

The whole reason his code didn't work was because he used document.form[0] instead of document.forms[0]!!!

Good eyes, Philip!
Follow Logic Ali's advice - use the error console!

Another good reason to use

var f = document.forms[0];

But as you say id is preferred

var f = document.getElementById("formid");
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-17-2012, 09:21 PM   PM User | #9
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
You don't need to give the form a name or id or even refer to it at all. You could just give the text boxes, check boxes etc an appropriate id or name and refer to them directly when validating them in your js.
minder is offline   Reply With Quote
Old 11-17-2012, 10:01 PM   PM User | #10
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
Yes, but why not simply insist that every <form> have an ID? ID's *MUST* be unique, so there's not even the possibility of a duplicated field name.
My suggestion was made on the basis of not amending the HTML for the current form. Using an id for accessing the form from JavaScript is certainly the better option.

Where you have an id on any field within the form you don't need one on the form tag itself as any field within the form provides easy access to the entire form.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-18-2012, 09:28 AM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by felgall View Post
My suggestion was made on the basis of not amending the HTML for the current form. Using an id for accessing the form from JavaScript is certainly the better option.

Where you have an id on any field within the form you don't need one on the form tag itself as any field within the form provides easy access to the entire form.
Very true, but if the form is to be submitted to a server-side script then the form elements must have names (in addition to ids if present). Like Old Pedant I see no real need to assign ids to form elements.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-18-2012, 09:45 AM   PM User | #12
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
Quote:
Originally Posted by Philip M View Post
Very true, but if the form is to be submitted to a server-side script then the form elements must have names (in addition to ids if present). Like Old Pedant I see no real need to assign ids to form elements.
But id's make it easier to reference the form elements in the css if you want to style them in a specific way.

So what you *NEED* to do comes down to preference because you don't *NEED* to reference the form at all either - by name and/or id or forms[x] - to access the form elements.

Last edited by minder; 11-18-2012 at 10:07 AM..
minder is offline   Reply With Quote
Old 11-18-2012, 12:01 PM   PM User | #13
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by minder View Post
But id's make it easier to reference the form elements in the css if you want to style them in a specific way.

So what you *NEED* to do comes down to preference because you don't *NEED* to reference the form at all either - by name and/or id or forms[x] - to access the form elements.
Yes, bullant. It is (as always) a case of horses for courses, or doing what is most suitable/appropriate/convenient in any particular situation.
That is not exactly the same as personal preference.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-18-2012, 12:35 PM   PM User | #14
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
Quote:
Originally Posted by Philip M View Post
....doing what is most suitable/appropriate/convenient in any particular situation.
That is not exactly the same as personal preference.
In this case it is exactly the same as personal preference because no way of accessing form elements suggested in this thread is better than the other. So which way you or I or old pedant or felgall think is best comes down to each of our personal preferences.

Being retired I no longer have to put up with other peoples' views on how things should be done. I can now freely do what I think is best when I have more than 1 option
minder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:34 AM.


Advertisement
Log in to turn off these ads.