We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11054
    • 59 Posts
    I’m working on a form where a person can select only a certain number of checkbox options. For instance, if there are 10 checkbox options, but they are only allowed to select three, is there a way in the form that you could create an error message when more than the alloted number have been choosen?

    I haven’t seen anything like this in the forums, but I do see that it is possible to do it with JavaScript. I found this script through a quick Google search:

    <script type="text/javascript">
    function chkcontrol(j) {
    var total=0;
    for(var i=0; i < document.form1.ckb.length; i++){
    if(document.form1.ckb[i].checked){
    total =total +1;}
    if(total > 3){
    alert("Please Select only three")
    document.form1.ckb[j].checked = false ;
    return false;
    }
    }
    } </script>

    Is there a better way to do this in eForm, or would you recommend using the Java?

    Thanks for your help in advance.

    UPDATE: With more research, I found a simple JQuery plugin (Trilemma: http://plugins.jquery.com/project/trilemma) that seems to work better, and is more simple to install. You merely assign a class to the fieldset.

    In the previous Java posted above, it required onClick tags be entered on each input.

    With the JQuery plugin, the form can be updated as needed, without much hassle.

    I’m still interested to know if eForm provides any support for this internally though. Please let me know if anyone has any ideas around this.

    Thanks.
      • 3749
      • 24,544 Posts
      I don’t think there’s any built-in support for this in eForm. The only other way I can think of is to use multiple groups of radio options styled to look like a single list (assuming that that would work for you -- probably not).
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting