?
Solved

jQuery: Checkbox Validation Alert

Posted on 2011-03-16
10
Medium Priority
?
685 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
Industry Leaders: 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

850 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