User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 456,234 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,762 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2900 | Replies: 5 | Solved
Reply
Join Date: Sep 2005
Location: St. Louis
Posts: 145
Reputation: kahaj is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Help Validating a Check Box

  #1  
Nov 14th, 2007
I'm trying to create a page that has a button at the end with a check box before it. Unless the check box is checked, it shouldn't carry through with the link. I absolutely cannot get it to work. I've been toying around with it for over a half hour now and can't get it working. I'm not sure that I actually need the <form> elements in there since it's not actually a form, but I can't begin to conceive how to make it work without the <form> elements, though. Here's what I've come up with at this point:

<!accept.html>
<html>
<head><title>Agreement Page</title>
<script type="text/javascript">
<!-- HIDE FROM IMCOMPATIBLE BROWSERS

function submitForm() {
	var submitForm = false;
	if (document.forms[0].accept.value == "")
		window.alert("You must agree with the above items in order to utilize the statistics form.");
		return false;
	else {
		return true;
	}
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script></head>
<body bgcolor="#CCCCCC">

<p><div align="center"><font size="4" face="Garramond">Before you go any further, you need to read the following and agree to it.<br />Otherwise, you cannot utilize the statistics form.</font></div></p><br /><br />

<p><font size="2" face="Arial">
<ul>
 <li>These pages have been developed for educational purposes only.</li>
 <li>No profit is intended to be made.</li>
 <li>The images contained herein are property of the National Football Leauge, the NFL Players Association, and the Dallas Cowboys.<br />The use of them is not intended as copyright infringement.</li>
 <li>These pages are not intended to aid or promote illegal gambling.  By using them, you (the user) will not try and accomplish this means.</li>
</ul>
</font></p><br /><br />

<p>Do you accept the terms as outlined above?<br />
<form action="" method="post" onsubmit="return confirmSubmit();">
<input type="checkbox" name="accept" value="Accepted" />I Accept<br /><br />
<input type="button" name="proceed" value="Proceed" onclick=window.location="nflStats.html";>
</form></p>
</body>
</html>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Location: Brisbane, Australia
Posts: 215
Reputation: sillyboy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Posting Whiz in Training

Re: Validating a Check Box

  #2  
Nov 14th, 2007
move you window redirecting code to a javascript function. Make your onclick call a function to validate the checkbox (again, in javascript), and then if the checkbox is checked you can then redirect.

Actually looking at your code, you seem to be mixing 2 approaches. You do not actually need the onclick if you set the input type to "submit" and the form action to "nflStats.html". Using this will trigger your confirmSubmit function too.

So there are 2 approaches you can take, I suggest you take the latter one I have mentioned.
Reply With Quote  
Join Date: Sep 2005
Location: St. Louis
Posts: 145
Reputation: kahaj is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: Validating a Check Box

  #3  
Nov 14th, 2007
I made some changes and it is still loading nflStats.html whether the check box is checked or not. Here is what I have right now:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<!--accept.html-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Agreement Page</title>
<script type="text/javascript">
<!-- HIDE FROM IMCOMPATIBLE BROWSERS

function confirmSubmit() {
	var submitForm = false;
	if (document.forms[0].accept.value == "")
		window.alert("You must agree with the above items in order to utilize the statistics form.");
		return false;
	else {
		return true;
	}
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script></head>
<body bgcolor="#CCCCCC">

<p><div align="center"><font size="4" face="Garramond">Before you go any further, you need to read the following and agree to it.<br />Otherwise, you cannot utilize the statistics form.</font></div></p><br /><br />

<p><font size="2" face="Arial">
<ul>
 <li>These pages have been developed for educational purposes only.</li>
 <li>No profit is intended to be made.</li>
 <li>The images contained herein are property of the National Football League, the NFL Players Association, and the Dallas Cowboys.<br />The use of them is not intended as copyright infringement.</li>
 <li>These pages are not intended to aid or promote illegal gambling.  By using them, you (the user) will not try and accomplish this means.</li>
</ul>
</font></p><br /><br />

<p>Do you accept the terms as outlined above?<br />
<form action="nflStats.html" method="post" onsubmit="return confirmSubmit();">
<input type="checkbox" name="accept" value="Accepted" />I Accept<br /><br />
<input type="submit" name="proceed" value="Proceed">
</form></p>
</body>
</html>

Where do I seem to be dropping the ball at on this one?
Reply With Quote  
Join Date: Mar 2007
Location: Brisbane, Australia
Posts: 215
Reputation: sillyboy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Posting Whiz in Training

Re: Validating a Check Box

  #4  
Nov 14th, 2007
Watch your blocks as I think you are missing a few { }.

I would do it slightly different to you, but this is how I would do it:

function confirmSubmit() {
        var acc = document.getElementById("accept");
	if (!acc.checked) {
		alert("You must agree with the above items in order to utilize the statistics form.");
		return false;
	} else {
		return true;
	}
}


For this to work with your code, just make sure you set the id for your checkbox to "accept".
Reply With Quote  
Join Date: Sep 2005
Location: St. Louis
Posts: 145
Reputation: kahaj is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: Validating a Check Box

  #5  
Nov 14th, 2007
Alright, it works now! I understand the "document.getElementById("accept");", but don't understand the (!acc.checked) part. I'm not familiar with what the !acc is about.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,012
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Validating a Check Box

  #6  
Nov 15th, 2007
var acc = document.getElementById('accept') assigns the reference of the checkbox element of your form to acc. Checkboxes have a property checked which determines whether a checkbox is checked or not.

Thus !acc.checked means 'when checkbox is not checked'.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 5:07 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC