I've added jQuery UI combobox with remote source to my form. Now, I am trying to validate that with jQuery Validation Plugin (only values from the list are allowed, field is mandatory).
I've tried standard approach:
$("#myform").validate({
focusInvalid: false,
focusCleanup: true,
rules: {
cbCountry: { // combobox
required: true
}
But still empty values are allowed. What am I doing wrong?
Update: I've tried to follow @Mike_Laird's advice below and I've found that my custom method
$.validator.addMethod('validComboboxValue', function (value) {
},
...
even not called when applied to jQuery UI combobox. But when I assign the same method to standard text input, it is called.
The basic idea is to set values on each of the select options, and then write a custom validator method to check the option values. Something like the following custom method will make some selection only from the select dropdown required.
jQuery.validator.addMethod(
"selectComboboxCheck",
function (value, element)
{
if (element.value === "0") {return false;}
else return true;
}
"Required - select an option."
);
Put class="selectComboboxCheck" in the select tag to trigger this method. The select options should look something like
<option value = "1">1</option>
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