ASP.NET MVC 2: Custom client side validation rule for dropdowns

Posted by Nigel on Stack Overflow See other posts from Stack Overflow or by Nigel
Published on 2010-05-20T00:29:43Z Indexed on 2010/05/20 0:40 UTC
Read the original article Hit count: 442

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about asp

Related posts about asp.net-mvc-2