ASP.NET MVC 2: Custom client side validation rule for dropdowns
- by Nigel
I have some custom validation that I need to perform that involves checking the selected option of a dropdown and flagging it as invalid should the user select a specific option.
I am using ASP.NET MVC 2 and have a custom Validator and custom server side and client side validation rules as described in this blog article. The server side validation is working fine, however, the client side validation is failing.
Here is the javascript validation rule:
Sys.Mvc.ValidatorRegistry.validators["badValue"] = function(rule) {
var badValue = rule.ValidationParameters["badValue"];
return function(value, context) {
if (value != badValue) {
return true;
}
return rule.ErrorMessage;
};
};
The rule is being applied to the dropdowns successfully, and placing a breakpoint within the returned function confirms that the validation is firing, and that the 'badValue' is being correctly set. However, 'value' is always null, and so the check always fails. What am I doing wrong?