【发布时间】:2011-11-23 02:00:13
【问题描述】:
我正在将站点从 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
}
}
【问题讨论】:
标签: jquery asp.net asp.net-mvc-3 validation