Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8837629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:46:32+00:00 2026-06-14T09:46:32+00:00

I am having trouble using jQuery validator.addMethod to validate check boxes based on class

  • 0

I am having trouble using jQuery validator.addMethod to validate check boxes based on class names as opposed to names.

Take a look at this and tell me why the script doesn’t work when placed within script tags inside the html.

The code is here: http://jsbin.com/ecozih/2

If you move the script over to the JavaScript window on JS bin it works.

If there is a easier way to do this, please let me know. An example would be greatly appreciated.

<html>
  <head>
<script src="hxxp://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="hxxp://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>

  </head>
<body>
<script>
$.validator.addMethod('interests', function (value) {
return $('.interests:checked').size() > 0; }, 'Pick one or more');
var checkboxes = $('.interests');
var checkbox_names = $.map(checkboxes, function(e,i) { return $(e).attr("name")}).join(" ");
$("#subscribeForm").validate({
    groups: { checks: checkbox_names },
    errorPlacement: function(error, element) {
             if (element.attr("type") == "checkbox")
               error.insertAfter(checkboxes.last());
             else
               error.insertAfter(element);
}
});
</script>
<form name="subscribeForm" id="subscribeForm" method="post">
<input type="checkbox" class="interests" value="1" name="group[1357][1]" ><label>Bananas</label>
<input type="checkbox" class="interests" value="2" name="group[1357][2]"><label>Oranges</label>
<input type="checkbox" class="interests" value="4" name="group[1357][4]"><label>Apples</label><br />
<label for="checks" generated="true" class="error" style=""></label><br />
<input type="submit" />
</form>
  </body>

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:46:34+00:00Added an answer on June 14, 2026 at 9:46 am

    You forgot to wrap your jQuery code within a document.ready. In other words, your jQuery is trying to select elements before the elements have even been constructed. The document.ready fixes this. Since the form does not yet exist when the code is called, the document.ready only fires off when the HTML DOM is ready.

    jsFiddle Demo: http://jsfiddle.net/TCHYJ/

    and your jsBin: http://jsbin.com/ecozih/4/edit

    Quote: “If you move the script over to the JavaScript window on JS bin it works.”

    That’s because, code inside the JavaScript panes of jsBin and jsFiddle wait for the DOM to load before being fired whether you use the document.ready or not.

    http://jsfiddle.net/TCHYJ/1/

    http://jsbin.com/ecozih/6/edit

    <script>
        $(document).ready(function() {
    
            $.validator.addMethod('interests', function(value) {
                return $('.interests:checked').size() > 0;
            }, 'Pick one or more');
            var checkboxes = $('.interests');
            var checkbox_names = $.map(checkboxes, function(e, i) {
                return $(e).attr("name")
            }).join(" ");
    
            $("#subscribeForm").validate({
                groups: {
                    checks: checkbox_names
                },
                errorPlacement: function(error, element) {
                    if (element.attr("type") == "checkbox") error.insertAfter(checkboxes.last());
                    else error.insertAfter(element);
                }
            });
    
        });​
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie on Rails and Javascipt. I've been having trouble using Jquery validate
Having trouble getting a form using PHP, JQuery Form plugin, and JQuery Validate plugin
I'm using jquery validate and I've set up groups. However, I'm having trouble figuring
I'm having trouble using jQuery.ajax() to post a gist to Github. The gist is
I'm using JQuery Mobile 1.1.0 and I'm having trouble keeping the underlying list selections
I'm using jQuery Datepicker but I'm having trouble when editing records. // js code
I am using jQuery UI Selectmenu and am having trouble setting the value of
Using jQuery 1.7 I'm having trouble binding a Click event to some dynamically loaded
I'm using the jQuery UI slider and having trouble saving the position of the
I'm using a Jquery fancydropdown menu for my navigation and am having trouble aligning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.