MVC 5失去验证 [英] MVC 5 Losing validator

查看:117
本文介绍了MVC 5失去验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到MVC 5(C#)与表单验证一个奇怪的行为。
这是我的code来检查KEYUP错误:

I have noticed a strange behavior in MVC 5 (C#) with the form validator. This is my code to check on keyup for errors:

    var $validatr = $('form').data('validator');
    var settngs = $validatr.settings;

    settngs.onkeyup = function (element, eventType) {
        if (!$validatr.element(element)) {
            $(this.currentForm).triggerHandler("invalid-form", [this]);
        }
    };
    settngs.onfocusout = false;

我已经注意到,这code适用于某些形式,对其他没有。我试图让验证器也是这样的:

I have noticed that this code works on some forms, on other not. I tried to get the validator also like this:

var $validatr = $('form').validate();

但它仍然没有工作。 重要:,然后我注意到,在code为工作仅当用户还没有登录报名表。 (我也可以访问登记表,当用户登录)。当用户登录我得到这个错误:

But it is still not working. Important: then I noticed that the the code is working for the registration form only if the user is not logged already. (I can access the registration form also when the user is logged). When the user is logged I get this error:

TypeError: $validatr is undefined

在这种情况下,用于将输入表格弹出,该错误仅当存在该元素的一个out_of_focus

In this case the error for the input form pops-up only when there is an out_of_focus of the element.

更新

如果我删除该cookie:AspNet.ApplicationCookie并刷新页面在用户注销,验证的onkeyup工作。这是怎么回事?

If I delete this cookie: AspNet.ApplicationCookie and refresh the page the user is logged out and the onKeyUp validation is working. What is going on?

推荐答案

如果你有一个页面上有多个表,或者您有一个包含页面上的表单j​​Query选择可以有多种形式有针对性你的方式弹出使用您的选择。我建议你​​通过标识针对一个窗体,而不是

If you have more than one form on a page or you have a popup that contains a form on the page the Jquery selector can have multiple forms targeted the way you are using your selector. I would suggest you target a form by Id instead

var $validatr = $('#myForm1').data('validator');

或者选择使用一个循环为目标的所有表单元素

Or alternatively using a loop to target all form elements

 $('form').each(function( index, form ) {
    var settngs = form.settings;

    settngs.onkeyup = function (element, eventType) {
        if (!form.element(element)) {
            form.triggerHandler("invalid-form", [this]);
        }
    };
    settngs.onfocusout = false;
});

这篇关于MVC 5失去验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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