2

I am using jquery form validation. I have a set of checkboxes generated from the database.

The checkboxes have the same name and same id. I am using name as "softappver[]" so when the form is posted I get it as an array. I would like to keep it that way.

I like to at least one of the checkboxes checked otherwise it should generate a message.

here is the code:

<script src="http://10.0.0.80/assets/js/jquery.js"></script>
<script src="http://10.0.0.80/assets/js/jquery.validate.js"></script>
<script>

jQuery.validator.addMethod( 
    "checkdropdown", 
    function(value, element) { 
        if (element.value == 0 || element.value == "#") { 
            return false; 
        } 
        else return true; 
    }, 
    "Please select an option." 
); 

$().ready(function() {  
    $("#jobform").validate({
        rules: {
            ptitle: {
                required: true,
                minlength: 2
            },

            vendor:{
               checkdropdown: true 
            },

            softwareapp:{
               checkdropdown: true 
            },



            softappver[]:{
               required: true,
            },


        },
        messages: {
            ptitle: "Please enter your job reference",          
            vendor: "Please select a vendor",
            softwareapp: "Please select a software",
            softappver: "Please select a application version"           

        }
    });
});
</script>

<form action="addeditprojectdetails" method="post" accept-charset="utf-8" class="jobform" id="jobform">
  Job reference :
  <input type="text" name="ptitle" value="" id="ptitle1"  />
  Primary role:
  <select name="primaryrole" id="primaryrole">
    <option value="0">Please select</option>
    <option value="1">Programme Manager</option>
    <option value="2">Project Manager</option>
    <option value="3">Test Manager</option>
    <option value="4">Business Analyst</option>
    <option value="5">Functional Consultant</option>
    <option value="6">Technical Architect</option>
    <option value="7">Data Migration Consultant</option>
    <option value="8">Test Analyst</option>
    <option value="9">Systems Tester</option>
    <option value="10">Analyst Programmer</option>
    <option value="11">Programmer</option>
    <option value="12">Systems Administrator</option>
    <option value="13">Trainer - Technical</option>
    <option value="14">Trainer - Functional</option>
    <option value="15">Technical Support</option>
  </select>
  Secondary role
  <select name="secondaryrole" id="secondaryrole">
    <option value="0">Please select</option>
    <option value="1">Programme Manager</option>
    <option value="2">Project Manager</option>
    <option value="3">Test Manager</option>
    <option value="4">Business Analyst</option>
    <option value="5">Functional Consultant</option>
    <option value="6">Technical Architect</option>
    <option value="7">Data Migration Consultant</option>
    <option value="8">Test Analyst</option>
    <option value="9">Systems Tester</option>
    <option value="10">Analyst Programmer</option>
    <option value="11">Programmer</option>
    <option value="12">Systems Administrator</option>
    <option value="13">Trainer - Technical</option>
    <option value="14">Trainer - Functional</option>
    <option value="15">Technical Support</option>
  </select>
  Vendor
  <select name="vendor" id="vendor">
    <option value="0">Please select</option>
    <option value="5">Microsoft</option>
    <option value="9">Apple</option>
    <option value="2">Oracle</option>
    <option value="7">Java</option>
  </select>
  Application
  <select name="softwareapp" id="softwareapp">
    <option value="0">Please select</option>
    <option value="2">OS</option>
    <option value="1">Development kit</option>
  </select>
  Versions
  <input type="checkbox" name="softappver[]" value="1" id="softappver" style="width:auto; max-width:auto;"  />  R4
  <input type="checkbox" name="softappver[]" value="8" id="softappver" style="width:auto; max-width:auto;"  />  R5
  <input type="checkbox" name="softappver[]" value="2" id="softappver" style="width:auto; max-width:auto;"  />  R6
  <input type="checkbox" name="softappver[]" value="3" id="softappver" style="width:auto; max-width:auto;"  />  R7
  <input type="checkbox" name="softappver[]" value="4" id="softappver" style="width:auto; max-width:auto;"  />  R8
  <input type="checkbox" name="softappver[]" value="5" id="softappver" style="width:auto; max-width:auto;"  />  R9  
  <input type="checkbox" name="softappver[]" value="6" id="softappver" style="width:auto; max-width:auto;"  />  R10
  <input type="checkbox" name="softappver[]" value="7" id="softappver" style="width:auto; max-width:auto;"  />  R11
</form>
2
  • Use same class name instead of id's with same name. Commented Jun 13, 2013 at 9:16
  • 1
    the key softappver[] is invalid use 'softappver[]' instead Commented Jun 13, 2013 at 9:19

2 Answers 2

4

The problem is there is a syntax error

        softappver[]: {
           required: true,
        },

should be

        "softappver[]": {
           required: true,
        },

Demo: Plunker

Sign up to request clarification or add additional context in comments.

Comments

1

You really shouldn't be giving those checkboxes the same ID. Use a class instead:

<input type="checkbox" name="softappver[]" value="1" class="softappver" style="width:auto; max-width:auto;"  />  R4
<input type="checkbox" name="softappver[]" value="8" class="softappver" style="width:auto; max-width:auto;"  />  R5
etc...

and then use something like this:

if ($(".softappver:checked").length < 1)
    alert("Please select at least one checkbox.");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.