Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Javascript function problem
Closed Thread
Old 03-23-2009, 06:15 PM   #1 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Javascript function problem

I am trying to get this code to work such that when you click the button, a pop-up box tells you that you have to check the checkbox first. There isn't actually a destination page yet, I am trying to just get the code to work right for the above to occur.

Can anyone tell me what is wrong with my code ? Thanks in advance for any help.

Code:
<html>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Check box practice HTML</title>

<meta http-equiv="content-type" content="text/html;
     charset=iso-8859-1" />


....I removed this part of the HTML for brevity.....

Do you agree to the terms above ? <br />

If so, please check the checkbox below.<h3>

<br />
<br />

<script type="text/javascript">
/* <![CDATA[ */
//code written by Eric Maddan

 


function chkBoxselected()  {
   if (document.terms.chkBox.checked == false)   {
     
      alert (' You didn't click the checkbox to confirm the rules listed above !');
      return false;
      }
   else
      {
      return true;
      }

}
 

/* ]]> */


</form>
</script>
</head>

<body>
<script type="text/javascript">
/* <![CDATA[ */



/* ]]> */

</script>


<form name="terms">
<input type="checkbox" name="chkBox" />  I accept the terms and conditions<br /><br />
<input type="button" name="close" value="Enter Site" size="75" 
    onclick="function chkBoxselected()";>

</form>
    
    

</body>
</html>

__________________
  • Operating system(s) : Windows 7 Home Premium x64 | Ubuntu 9.10
  • CPU : Pentium Dual-Core E5200 @ 2.50GHz /Socket 775 LGA
  • Memory: 2X 2GB DDR2 SDRAM
  • Hard drive: 640 GB Western Digital Caviar Blue 7200rpm WD6400AAKS-75
  • Motherboard : Dell 0U880P
  • Video: nVidia GeGorce 8400GS
  • Chipset : Intel G41

<<<<< If you found anything I said helpful, please click the or under my avatar
oldskool is offline  
Old 03-23-2009, 06:27 PM   #2 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Javascript function problem

Code:
<html>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Check box practice HTML</title>

<meta http-equiv="content-type" content="text/html;
     charset=iso-8859-1" />


....I removed this part of the HTML for brevity.....

Do you agree to the terms above ? <br />

If so, please check the checkbox below.<h3>

<br />
<br />

<script type="text/javascript">
/* <![CDATA[ */
//code written by Eric Maddan

function validateForm(theForm)
{
	var reason = "";

	reason += validateCheckbox(theForm.chkBox);	  

	if (reason != "") {
		alert("Please fix the errors below!\n\n" + reason);
		return false;
	}
}

function validateCheckbox(el)
{
	var error = "";

	if (el.checked == false) {
		error = "You didn't click the checkbox to confirm the rules listed above !";
	}

	return error;
}
 
</script>
</head>

<body>
<script type="text/javascript">
/* <![CDATA[ */



/* ]]> */

</script>


<form name="terms" onsubmit="return validateForm(this);">
<input type="checkbox" name="chkBox" />  I accept the terms and conditions<br /><br />
<input type="submit" name="close" value="Enter Site" size="75" />

</form>
    
    

</body>
</html>
Try this.

I changed your button to a submit, and added an onsubmit argument to the form. Basically, when you press submit, the function in onsubmit will run. If it returns false (in the case of an error), the form won't submit... but if it returns true, the form will submit. The function checks another function that validates if the check box is pressed or not.

The advantage to my way is that you can add more validation functions (for other parts of a form, maybe) very easily.
__________________

Need website help? PM me!
CrazeD is offline  
Old 03-23-2009, 06:39 PM   #3 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,158

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: Javascript function problem

onclick would also work but you need to bring the semicolon into the double quotes and escape the apostrophe in "didn't"

EDIT: also remove the word function from the onclick atribute
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page

Last edited by kmote; 03-23-2009 at 06:41 PM.
kmote is online now  
Old 03-23-2009, 06:39 PM   #4 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Re: Javascript function problem

Well that was some quick response ! Thanks, CrazeD. Ok, I see where you are going with that. I appreciate your help very much.

Quote:
Originally Posted by kmote View Post
onclick would also work but you need to bring the semicolon into the double quotes and escape the apoatrophe in "didn't"
So the code I had would have worked if I had made those changes, kmote ?
__________________
  • Operating system(s) : Windows 7 Home Premium x64 | Ubuntu 9.10
  • CPU : Pentium Dual-Core E5200 @ 2.50GHz /Socket 775 LGA
  • Memory: 2X 2GB DDR2 SDRAM
  • Hard drive: 640 GB Western Digital Caviar Blue 7200rpm WD6400AAKS-75
  • Motherboard : Dell 0U880P
  • Video: nVidia GeGorce 8400GS
  • Chipset : Intel G41

<<<<< If you found anything I said helpful, please click the or under my avatar

Last edited by oldskool; 03-23-2009 at 06:41 PM. Reason: Editing
oldskool is offline  
Old 03-23-2009, 06:45 PM   #5 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,158

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: Javascript function problem

Works like a charm mate.
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is online now  
Old 03-23-2009, 06:50 PM   #6 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Re: Javascript function problem

Thank you, then. I am glad to know that I am making some progress. Thanks to you and CrazeD, and jaeusm at times as well. You guys rock !
__________________
  • Operating system(s) : Windows 7 Home Premium x64 | Ubuntu 9.10
  • CPU : Pentium Dual-Core E5200 @ 2.50GHz /Socket 775 LGA
  • Memory: 2X 2GB DDR2 SDRAM
  • Hard drive: 640 GB Western Digital Caviar Blue 7200rpm WD6400AAKS-75
  • Motherboard : Dell 0U880P
  • Video: nVidia GeGorce 8400GS
  • Chipset : Intel G41

<<<<< If you found anything I said helpful, please click the or under my avatar
oldskool is offline  
Old 03-24-2009, 09:52 PM   #7 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Re: Javascript function problem

Quote:
Originally Posted by oldskool View Post
Thank you, then. I am glad to know that I am making some progress. Thanks to you and CrazeD, and jaeusm at times as well. You guys rock !
EDIT: Ok, I got it going now, here it is :

Code:
<html>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Check box practice HTML</title>

<meta http-equiv="content-type" content="text/html;
     charset=iso-8859-1" />


<br />
<br />
<br />

<center><h3>Do you agree to the terms above ? <br />

If so, please check the checkbox below.<h3></center>

<br />
<br />


<script type="text/javascript">
/* <![CDATA[ */
//code written by Eric Maddan


function chkBoxselected()  {
   if (document.terms.chkBox.checked == false)   {
     
      window.alert(" You did not click the checkbox to confirm the rules listed above ");
      return false;
      }
   else
      {
      return true;
      }

}
 

/* ]]> */


</script>
</head>

<body>
<script type="text/javascript">
/* <![CDATA[ */



/* ]]> */

</script>

<center>
<form name="terms">
<input type="checkbox" name="chkBox" />  I accept the terms and conditions<br /><br />
<input type="button" name="close" value="Enter Site" size="75" 
    onClick="chkBoxselected()"></center>

</form>
    
    

</body>
</html>
ATTN: Admins, this thread can be closed now
__________________
  • Operating system(s) : Windows 7 Home Premium x64 | Ubuntu 9.10
  • CPU : Pentium Dual-Core E5200 @ 2.50GHz /Socket 775 LGA
  • Memory: 2X 2GB DDR2 SDRAM
  • Hard drive: 640 GB Western Digital Caviar Blue 7200rpm WD6400AAKS-75
  • Motherboard : Dell 0U880P
  • Video: nVidia GeGorce 8400GS
  • Chipset : Intel G41

<<<<< If you found anything I said helpful, please click the or under my avatar

Last edited by oldskool; 03-24-2009 at 10:16 PM.
oldskool is offline  
 
Closed Thread

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Flash Actionscript 3, Javascript FlashVar Problem! ! Whitey ! Programming Discussions 3 02-16-2009 07:52 PM
Problem installing software in windows xp pro imagesmith Windows Operating Systems and Software 3 02-20-2008 02:46 PM
Problem with 3D graphics mordi05 Windows Operating Systems and Software 6 12-10-2007 11:37 PM
Computer problem ankit3000 Hardware Troubleshooting 6 06-06-2007 04:42 PM