0

ok the following code works fine if i put the checkbox room field without [] but if i do like this

$(function() {

// Setup form validation on the #register-form element
$("#reservation").validate({

    // Specify the validation rules
    rules: {
        room[]: "required",
        start: "required",
        end: "required",
        name: "required",
        phone: "required",
        email: {
            required: true,
            email: true
        },
        address: "required"
    },

    // Specify the validation error messages
    messages: {
        room[]: "    Select one room",
        start: "Select Check In date in Room Availability",
        end: "Select Check Out date in Room Availability",
        name: "Enter your full name",
        phone: "Phone number is required",
        email: "Please enter a valid email address",
        address: "Address is required"
    },


    submitHandler: function(form) {
        form.submit();
    }
});

});

then the room field with room[] is not throwing any error to select one room.

Seeker
  • 47
  • 7

1 Answers1

-1

http://jqueryvalidation.org/files/demo/radio-checkbox-select-demo.html

Update with working example:

http://codepen.io/anon/pen/RaGbeP

$(function() {
$('#myform').validate({
rules: {
  "room[]": {
    required: true
  }
},
messages: {
  "room[]": {
    required: "Please select a room<br/>"
  }
},
errorPlacement: function(error, element) {
  if (element.is(":radio")) {
    error.appendTo(element.parents('.container'));
  } else { // This is the default behavior 
    error.insertAfter(element);
  }
}
});

});

<form id="myform">
  <label for='room'>Select one room:</label>
  <p class='container'>
  <label><input type='checkbox' name='room[]' value='room one' />one</label>
  <label><input type='checkbox' name='room[]' value='room two' />two</label>
  <label><input type='checkbox' name='room[]' value='room three' />three</label>
</p>
<input type="submit" name="submit" value="Submit">
</form>
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • but i have multiple values to my checkbox – Seeker Mar 12 '16 at 10:13
  • The OP is dealing with checkboxes, not radio buttons. PHP depends on having `[]` in the name in order to accept multiple values for a single name. Removing the `[]` will break things. – Quentin Mar 12 '16 at 10:25
  • @Stuart the fields are checkbox as user gets option to select multiple options so it is required to have the input field as checkbox so the name should be like name="room[]" – Seeker Mar 12 '16 at 10:27
  • We are talking about jquery validation here, not the php side of things! I said to remove the [] from the validation rules as you can see above, I've edited the HTML for clarity... Apologies for not showing them in the initial code paste. – Stuart Mar 12 '16 at 10:28
  • yes @Stuart its jquery validation thought i know i am using it for php but i needed it to validate checkbox with name="room[]" – Seeker Mar 12 '16 at 10:34
  • Apologies, I think I'm going crazy... on checkboxes, you can add the [] but make sure to quote it, updated code above – Stuart Mar 12 '16 at 10:37
  • i tried this but its not throwing any error message ` rules: { room: { required: true } start: "required", end: "required", name: "required", phone: "required", email: { required: true, email: true }, address: "required" }, ` – Seeker Mar 12 '16 at 10:46
  • @Stuart it works greate thanks soo much :) – Seeker Mar 12 '16 at 10:56
  • Glad to be if help - got there in the end. Thanks for marking as accepted answer even after someone else down voted :) – Stuart Mar 12 '16 at 11:35