我正在将一个站点从asp.net mvc 2转换为asp.net mvc 3.我想在mvc 3中使用内置验证,它使用jquery.validate和jquery.validate.unobtrusive。但是在我的旧网站上,我已经使用了jquery.validate并添加了自定义方法进行验证,然后我会在下拉列表更改时调用。
我需要能够:
这是我的asp.net 2网站上的代码。
//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
}
}