Solved

jQuery: Checkbox Validation Alert

Posted on 2011-03-16
10
676 Views
Last Modified: 2012-05-11
Hi there,

I am new to jQuery and am having some troubles with validation.  I have a checkbox to agree to our policy and I want an alert(); to happen if the user submits the page without having checked it.  Right now it will display an text message on the page if they haven't checked the checkbox, which I want, but I also want that alert() to happen.

What am I doing wrong?  The below code doesn't function at all, but how would I change it to get the desired effect?

Thanks.
jQuery.validator.addMethod("agree", function(value, element) {
	if ($('#TERMSAGREEMENT').val() != "on") { 
alert("Please click to agree."); }	
			
	}, "");

....

<input name='terms' id='terms' class="required agree"  type='checkbox'>

Open in new window

0
Comment
Question by:maeve100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 29

Expert Comment

by:Badotz
ID: 35152563
Shouldn't #TERMSAGREEMENT be terms?
0
 

Author Comment

by:maeve100
ID: 35152571
Sorry, I was trying to clean it up so it was shorter :-).  Yes - #TERMSAGREEMENT should be terms.  But it still doesn't work :-).
0
 

Author Comment

by:maeve100
ID: 35152580
Sorry about that :-).
jQuery.validator.addMethod("agree", function(value, element) {
	if ($('#terms').val() != "on") { 
alert("Please click to agree."); }	
			
	}, "");

....

<input name='terms' id='terms' class="required agree"  type='checkbox'>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Badotz
ID: 35152601
Well, in "normal" JavaScript:
function test_it() {
    var val = document.getElementById("terms").checked;
    
    if (val == "checked") {
        // True
    }
    else {
        // False
    }
}

Open in new window

0
 

Author Comment

by:maeve100
ID: 35152649
Yes, I was able to get that much working, but I am trying to get this to work under jQuery as I described above.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 35152666
You might try
if ($('#terms').checked() != "checked") { 

Open in new window

0
 

Author Comment

by:maeve100
ID: 35152792
I have tried that, it still doesn't come up with the alert being displayed on submit if terms is unchecked.
0
 
LVL 29

Accepted Solution

by:
Badotz earned 500 total points
ID: 35175340
In the following
jQuery.validator.addMethod("agree", function(value, element) {

Open in new window

what are value and element? Perhaps their values can help?
0
 

Author Closing Comment

by:maeve100
ID: 35288486
I tried to cancel this question but you kept it open, I was not looking for a JavaScript solution, so I was trying to re-post this in the right category.  I did so and got the answer I was looking for.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 35290602
If none of the solutions worked, why dod you award points?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question