我在 MVC4 场景中有以下 jQuery 验证器。它工作正常。但是只有当用户单击按钮时我才需要验证;不是当用户更改文本框焦点时。我们如何做出这种改变?
jQuery
$(function ()
{
jQuery.validator.unobtrusive.adapters.add
("lijovalidator", ["param"], function (options)
{
options.rules["lijovalidator"] = options.params.param;
options.messages["lijovalidator"] = options.message;
});
jQuery.validator.addMethod("lijovalidator",
function (value, element, param) {
alert(value);
alert(param);
alert(element);
alert($("#" + param).val());
return false;
});
}(jQuery));
SelectedValueCheckAttribute 中的客户端验证
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule mcvr = new ModelClientValidationRule();
mcvr.ValidationType = "enteredvaluescheck";
mcvr.ErrorMessage = "Selected Value must be in given range";
mcvr.ValidationParameters.Add("param", OtherProperty);
mcvr.ValidationType = "lijovalidator";
yield return mcvr;
}
模型
public class CrossFieldValidation
{
public IEnumerable<SelectListItem> Items { get; set; }
[SelctedValueCheckAttribute("TxtCrossField") ]
public string SelectedValue { get; set; }
[Required]
[Display(Name = "Quantity:")]
public string TxtCrossField{ get; set; }
public IEnumerable<SelectListItem> ShippingItems { get; set; }
[SelctedValueCheckAttribute("ShippingValue")]
public string SelectedShippingItemValue { get; set; }
[Display(Name = "Quantity:")]
[Required]
public string ShippingValue { get; set; }
}