Solved

Using jQuery validation plugin, need to ensure at least one checkbox is checked when names are different

Posted on 2009-03-31
4
3,312 Views
Last Modified: 2012-05-06
Hi Experts,

On a web form, I have a set of checkboxes that form a logical group but do not have the same name. Using the jQuery validation plugin (http://docs.jquery.com/Plugins/Validation), I need to add a custom validation method and rule to ensure that the user checks at least one checkbox.

I've read the documentation on jquery.com and understand the basics of adding a method and rule, but I haven't been able to work out the specifics for this particular scenario.

Fair warning :) I already found http://osdir.com/answers/javascript/16692-jquery-validation-plugin-validate-checkboxes-dont-have-samename.html and this does *not* provide the detail I'm looking for. Points won't be given for a reference to this page or anything similar, or for an answer that says, "Write a custom rule." I already figured that part out :) I set the points higher because I'm looking for actual code for the custom method and rule.

Thanks in advance--

Kathryn
<input type="checkbox" name="length" value="length">

<input type="checkbox" name="density" value="density">

<input type="checkbox" name="packaging" value="packaging">

<input type="checkbox" name="loose" value="loose">

Open in new window

0
Comment
Question by:KathrynGZ
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:anoyes
ID: 24033303
I think this should work for you.  You might also want to look into doing custom error positioning, so that only one error shows up, perhaps at the start of the checkbox list, which is also included in the script below.

<script type="text/javascript">

$.validator.methods.checkboxGroup = function(value,element,param)

{

  return (

    $("input[name='length']").is(':checked') ||

    $("input[name='density']").is(':checked') ||

    $("input[name='packaging']").is(':checked') ||

    $("input[name='loose']").is(':checked')

  );

}
 

$('form').validate({

  rules: {

    length: "checkboxGroup",

    density: "checkboxGroup",

    packaging: "checkboxGroup",

    loose: "checkboxGroup",

    //etc

  },

  errorPlacement: function(err, el) {

    var name = $(el).attr('name');

    if (name == 'length' || name=='density' || name=='packaging' || name=='loose')

    {

      err.insertBefore(el);

    }

    else

    {

      err.insertAfter(el);

    }

  }

});

</script>

Open in new window

0
 

Author Comment

by:KathrynGZ
ID: 24033654
anoyes,

YOU ROCK! That's exactly what I was looking for. Thank you!
 
Just one more detail: it's telling me no error message was defined. Do you know how to add that? (I know how to add a message with validator.addMethod, but the same thing didn't work with validator.methods).

Thank you!

Kathryn
0
 
LVL 16

Accepted Solution

by:
anoyes earned 350 total points
ID: 24033743
You know, I don't know how to go about setting a default message for that method of creating a custom validator; I usually just set it up for the elements in the messages: {} object.  However, we can do the same thing pretty much with the addMethod function.

<script type="text/javascript">

$.validator.addMethod("checkboxGroup", function(value,element) {

  return (

    $("input[name='length']").is(':checked') ||

    $("input[name='density']").is(':checked') ||

    $("input[name='packaging']").is(':checked') ||

    $("input[name='loose']").is(':checked')

  ),

  "Please select one..."

}

 

$('form').validate({

  rules: {

    length: "checkboxGroup",

    density: "checkboxGroup",

    packaging: "checkboxGroup",

    loose: "checkboxGroup",

    //etc

  },

  errorPlacement: function(err, el) {

    var name = $(el).attr('name');

    if (name == 'length' || name=='density' || name=='packaging' || name=='loose')

    {

      err.insertBefore(el);

    }

    else

    {

      err.insertAfter(el);

    }

  }

});

</script>

Open in new window

0
 

Author Closing Comment

by:KathrynGZ
ID: 31564965
If I could have given you an A+, I would have :) Thanks again!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 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

9 Experts available now in Live!

Get 1:1 Help Now