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

查看:109
本文介绍了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?

$(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.

任何想法?谢谢!

推荐答案

玉家伙,

我在同样的问题,发现这个线程:<一href=\"http://old.nabble.com/-validate--onkeyup-for-single-field-td21729097s27240.html\">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天全站免登陆