I have a form using JQuery validation plugin.
<label>
<input name="Distribution[]" type="checkbox" id="dist_europe" class="required minlength:1" value="Europe" /> Europe
</label>
<select name="Europe[]" size="5" multiple id="Europe">
<option value='Albania'>Albania</option>
<option value='Andorra'>Andorra</option>
<option value='Austria'>Austria</option>
</select>
I need to do 'conditional' validation. eg. If checkbox is selected, make sure at least one item in 'select option is selected'.
$(document).ready(function(){
$("#commentForm").validate({
rules: {
Europe: {
required: "#dist_europe:checked",
minlength: 1
}
},
messages: {
Europe: "Please select at least 1 country"
}
}
})
The issue I am facing now is :
How do I work around this? Thanks
$(document). ready(function(){ $("#commentForm"). validate({ rules: { 'Europe[]': { required: "#dist_europe:checked", minlength: 1 } }, messages: { 'Europe[]': "Please select at least 1 country" }, debug: true }); }); You can test it out here.
MultiSelect is a jQuery plugin that transforms a normal multi-select list into a multi-column dropdown list with checkboxes.
Validation in JQuery: Using JQuery, a form is validated on the client-side before it is submitted to the server, hence saves the time and reduce the load on the server. Form Validation means to validate or check whether all the values are filled correctly or not.
When using a name like name="Europe[]"
, you'll need to use a string for your identifier (the identifier is the name, not the id of the element) in rules
and messages
, like this:
$(document).ready(function(){
$("#commentForm").validate({
rules: {
'Europe[]': {
required: "#dist_europe:checked",
minlength: 1
}
},
messages: {
'Europe[]': "Please select at least 1 country"
},
debug: true
});
});
You can test it out here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With