Skip to content

Instantly share code, notes, and snippets.

@Braunson
Created February 8, 2015 20:48
Show Gist options
  • Save Braunson/a6ed85cc3d189dce2ce9 to your computer and use it in GitHub Desktop.
Save Braunson/a6ed85cc3d189dce2ce9 to your computer and use it in GitHub Desktop.
Custom jQuery Validator Methods/Rules for select drop downs.
// Custom validator method
$.validator.addMethod("valueNotEqualsVal", function(value, element, arg){
return arg != value;
}, "Value must not equal arg.");
$.validator.addMethod("valueNotEqualsTxt", function(value, element, arg){
return return arg != jQuery(element).find('option:selected').text();
}, "Value must not equal arg.");
// Usage
$("#someForm").validate({
rules: {
type: {
valueNotEqualsVal: "Choose..."
},
size: {
valueNotEqualsTxt: "Choose..."
}
},
messages: {
type: {
valueNotEqualsVal: "Please select a type"
},
size: {
valueNotEqualsTxt: "Please select a size"
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment