Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate jQuery UI combobox values with jQuery Validation Plugin?

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.

like image 259
LA_ Avatar asked Nov 20 '11 14:11

LA_


1 Answers

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>
like image 173
Mike_Laird Avatar answered Sep 18 '22 17:09

Mike_Laird