Created
February 8, 2015 20:48
-
-
Save Braunson/a6ed85cc3d189dce2ce9 to your computer and use it in GitHub Desktop.
Custom jQuery Validator Methods/Rules for select drop downs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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