ASP.net MVC 3 jQuery 验证;禁用不显眼的 OnKeyUp? [英] ASP.net MVC 3 jQuery Validation; Disable Unobtrusive OnKeyUp?

查看:21
本文介绍了ASP.net MVC 3 jQuery 验证;禁用不显眼的 OnKeyUp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法禁用某个验证器(信用卡)的 jQuery 验证,使其只发生在 onblur,而不是 onkeyup?

Is there a way to disable the jQuery Validation for a certain validator (creditcard) so that it only occurs onblur, instead of onkeyup?

基于 jQuery Validator 文档,我认为我可以这样做:

Based on the jQuery Validator documentation I thought I could do something like this:

$(function () {
    $("[data-val-creditcard]").validate({
        onkeyup: false
    })
});

但是,它似乎不起作用.

However, it doesn't seem to be working.

我还尝试在我的验证器上执行以下操作:

I also tried doing the following on my validator:

public class CreditCardValidator : DataAnnotationsModelValidator<CreditCardAttribute>
{
    string _message;

    public CreditCardValidator(ModelMetadata metadata, ControllerContext context, CreditCardAttribute attribute)
        : base(metadata, context, attribute)
    {
        _message = attribute.ErrorMessage;
    }

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = _message,
            ValidationType = "creditcard"
        };
        rule.ValidationParameters.Add("onkeyup", false);
        return new[] { rule };
    }
}

它也不起作用,但我只是尝试正确使用 ValidationParameters.

It doesn't work either, but I was just taking a stab at the appropriate use of ValidationParameters.

在表单中输入信用卡号,然后随机从无效变为有效,然后又变为无效,这有点烦人.

It is kind of annoying to be entering a credit card number in a form and having it randomly change from invalid to valid, then back to invalid.

有什么想法吗?谢谢!

推荐答案

好的,

我遇到了同样的问题并找到了这个帖子:http://old.nabble.com/-validate--onkeyup-for-single-field-td21729097s27240.html

I was in the same problem and found this thread: http://old.nabble.com/-validate--onkeyup-for-single-field-td21729097s27240.html

这个想法基本上是覆盖keyup事件并返回false.因此,在您的特定情况下,您需要添加:

The idea is basically to overwrite the keyup event and return false. So in your specific case you'll need to add:

$('#my-form').validate({
   rules: {
     [...]
   }
} 
// Disable keyup validation for credit card field
$("[data-val-creditcard]").keyup(function() { return false } );

并且您会看到您的信用卡字段仅在模糊或提交时进行检查(但其余部分也在使用 keyup).

And you'll see that your credit card field is only checked on blur or submit (but the rest is working on keyup also).

我一直在寻找相同的解决方案,发现这里的答案可以改进,所以我想在这里分享它会很好.

I was looking for the same solution, and found that the answer here could be improved, so I thought it would be nice to share it here.

这篇关于ASP.net MVC 3 jQuery 验证;禁用不显眼的 OnKeyUp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆