0

I am converting a site from asp.net mvc 2 to asp.net mvc 3. I am wanting to use the built in validation in mvc 3 which uses the jquery.validate and the jquery.validate.unobtrusive. However on my old site I was already using jquery.validate and added custom method for validation that I would then call when a drop down was changed.

I need to be able to:

  1. Register this method.
  2. Call only when the dropdown is changed.

Here is my code for this on my asp.net 2 site.

//add class to the html element
$("#ClientId-input").addClass("validClient");

//create the method "validClient"
$.validator.addMethod("validClient", function(value, element) {

    //get the actual value in the input box
    var _value =  $("#ClientId").data('tComboBox').value();

    //get all the items in the list
    var _items = $("#ClientId").data('tComboBox').data;

    //set the value to return
    var _retVal = false;

    //loop through the items if the selected value equals a value we are valid
    $.each(_items, function(index, value)
    {
        if(value.Value == _value){
            _retVal = true;

            //return false in the loop to break.
            return false;
        }
    });

    return _retVal;

}, "Please choose a Client from the list.");


//Assign the rule to the validator
$.validator.addClassRules({
    validClient:{validClient:true}
});


//this is called when dropdownchanges
function ClientsChanged(e)
{

    if($("#ClientId-input").valid())
    {
        //do work here
    }
}

1 Answer 1

0

In my answer here, at the bottom, you will see code showing how to add and register a custom validation method, so that MVC 3 unobtrusive validation will handle your custom validator.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.