Solved

jQuery: Checkbox Validation Alert

Posted on 2011-03-16
10
673 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iframe detection of parent window scale 20 60
Reference Error with plugin 1 31
How can I run the php script or the page without visiting the page? 6 53
Javascript closure 1 36
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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 about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now