Solved

jQuery: Checkbox Validation Alert

Posted on 2011-03-16
10
671 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 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

10 Experts available now in Live!

Get 1:1 Help Now