Custom Server and Client Side Required Validator in MVC 2 using jQuery.validate

This post has been moved here.

6 Responses to “Custom Server and Client Side Required Validator in MVC 2 using jQuery.validate”

  1. nikhiluppaluri Says:

    Mice article jwwishart !!!

    Im a newbie to MVC , I have a problem with my Cutom Model Validation , Hope you will help me to sort out.

    My Custom Model class have a property for DropDownList :

    [SelectOptionRequired(“OldPosition”,ErrorMessage = “Please select your old position.”)]
    [DisplayName(“Old Position”)]
    public SelectListItem OldPosition { get; set; }

    I have a custom class named SelectOptionRequiredAttribute.cs which implements ValidationAttribute class for DropDownList “Required” validation.

    i have overriden IsValid method like this :

    public override bool IsValid(object value)
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);

    if (properties.Find(“value”, true).GetValue(value) == null)
    return false;
    if (properties.Find(“value”, true).GetValue(value).ToString() == “”)
    return false;
    return true;


    Which is working fine for the server side , Now i want to display the validation message at client side , So i have followed the above procedure which you mentioned .

    I have created a custom class :

    JSelectOptionRequired which overrides the GetClientValidationRules() method like this :

    public override IEnumerable GetClientValidationRules()
    var rule = new ModelClientValidationRule { ErrorMessage = ErrorMessage, ValidationType = “jselectoptionrequired” };
    return new[] { rule };

    now i have added the method to MicrosoftMvcJQueryValidation.js

    jQuery.validator.addMethod(“jselectoptionrequired”, function(value, element) {
    if (value == undefined || value == null)
    return false;

    if (value.toString().length == 0)
    return false;

    return true;

    and the other JQuery file i have included to my View.

    But Client Side Validation is not working.

    Could please help me to sort out this issue ?

    Thanks in Advance.

  2. jwwishart Says:

    @Nikhil – I would do a few things to try and determine what is happening.

    Check the variable that MVC renders: look at the source MVC is rendering and search for the following string “window.mvcClientValidationMetadata.push” you should see a javascript array containing all the validation rules including the one that you return from the GetClientValidationRules() method.

    If your custom rules are there then if you have firebug I suggest you open it up and put a breakpoint on the first line inside the jQuery.validator.addMethod … line… And see if it is firing…

    That is where i would start… You need some more information I think (Sorry if you’ve already done all this! Fill me in with the details if so!)


  3. nikhiluppaluri Says:

    Thank you very much jwwishart.

    Yes!! We have all the validation rules in “window.mvcClientValidationMetadata.push” and Yes it is hitting inside the jQuery.validator.addMethod() , After tracing i found this issue :

    Im declaring my dropDownLitst like this in my View Page :

    model.OldPosition.Value, GenericLists.positionStatusList, “–select–“)%>

    and i have updated like this (removed the Value property)

    model.OldPosition, GenericLists.positionStatusList, “–select–“)%>

    now my Client Side validation working fine , But my server side validation stops working, Since im using the same View for both Add/Update i need the Value property to bind the selected list value.

    Please let me know what is the issue.


  4. nikhiluppaluri Says:

    model.OldPosition, GenericLists.positionStatusList, “–select–“)%>

  5. ASP.NET MVC Archived Blog Posts, Page 1 Says:

    […] to VoteCustom Server and Client Side Required Validator in MVC 2 using jQuery.validate (3/21/2010)Sunday, March 21, 2010 from jwwishartUpdate: Added information on issues that I’ve still yet to […]

  6. jwwishart Says:

    @nikhiluppaluri sorry for not replying earlier. Are you using the Html.DropDownListFor helper method in the above code you’ve given. It’s a bit hard to see what exactly you are doing (maybe due to the WordPress altering your code.)

    Have you solve this issue yet?

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: