views:

12

answers:

1

I have a category dropdown selection element in my form. The form is validated with jQuery as follows:

    $('#myForm').validate({
    rules: {
        text: {
              required: true
        },
        category: {
            required: true
        }
    },
    messages: {
        text: {
            required: "Text required"
        },
        category: {
            required: "Category required"
        }
    }

When loading the page, the default value of category is: '---' (without quotes). When I submit the form, the validation says eveything is ok, because '---' is not nothing. How can I get it to stop submitting when '---' is used as category? The other categories are something like this: Category1. One of the possibilities have to be choosen and I can't change the dropdown selection thing.

Thanks!!

+1  A: 

You could add a custom rule, like this:

jQuery.validator.addMethod("notEqual", function(value, element, param) {
  return this.optional(element) || value !== param;
}, "Please choose a value!");

Then use that rule in your rules, like this:

    category: {
        required: true,
        notEqual: "---"
    }

You can test it out here.

Nick Craver
Thanks a lot for the quick reply!!
koko