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:
- Register this method.
- 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
}
}