Solved

jQuery: Checkbox Validation Alert

Posted on 2011-03-16
10
675 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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…

830 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