如何避免jQuery验证器在文本框选项卡上触发

时间:2013-07-31 08:03:31

标签: jquery asp.net-mvc asp.net-mvc-4 jquery-validate unobtrusive-validation

我在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));

SelctedValueCheckAttribute中的客户端验证

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; }



    }

1 个答案:

答案 0 :(得分:0)

使用如下:

$(function(){
   $(window).blur(function(){
      return true;
   });
});