According to the accepted answer here this is what I have to do to make required selects work with the validation.
I have created a sample on jsfiddle, but I can’t get it work.
The main difference is that my default is simply empty (“”).
<form id="myform">
<select id="id_deals-0-currency" class="required" name="deals-0-currency">
<option value="">---------</option>
<option selected="selected" value="1">USD - $</option>
<option value="2">EUR - €</option>
</select>
</form>
$(function() {
$(document).ready(function() {
$.validator.addMethod("valueNotEquals", function(value, element, arg) {
return arg != value;
}, "Value must not equal arg.");
$("#myform").validate({
rules: {
deals-0-currency: {
valueNotEquals: ""
}
},
messages: {
deals-0-currency: {
valueNotEquals: "Please select an item!"
}
}
});
});
})
Your code works, with exception that you need to quote
deals-0-currencyas an object key and your empty string default is being trumped by therequiredclass which makes the item required in validation. An empty value will trigger the required message first always and your validation method will never get triggered with current default of""DEMO ( with required class removed) http://jsfiddle.net/WGL4j/7/
EDIT: To validate the select when it is changed use the
validator valid()method which can be called on the whole form or individual elements only:Updated demo: http://jsfiddle.net/WGL4j/8/