Asp.Net MVC - JQuery 远程验证

发布于 2024-09-19 23:20:49 字数 1209 浏览 4 评论 0原文

我得到了以下 JQuery 验证脚本:

<script type="text/javascript">
$(document).ready(function() {
    $("#myForm").validate({
        rules: {
            "Content": {
                required: true,
                rangelength: [20, 1000],
                remote: {
                    url: "/RemoteValidation/IsValidContent",
                    timeout: 2000,
                    type: "post"
                }
            }
        },
        messages: {
            "Content": {
                required: "* ...A",
                rangelength: "* ...B",
                remote: "* ...C"
            }
        }
    });
});

以及以下控制器:

public class RemoteValidationController : Controller
    {
        [HttpPost]
        public virtual JsonResult IsValidContent(MyObject object)
        {    
            return new JsonResult
            {
                Data = true
            };
        }
}

它仅用于测试目的,它始终返回 true。

我遇到的问题如下。我看到错误消息 (...C) 出现 1 秒然后消失...

我不知道如何解决...

为什么它会出现...它不应该出现...

更新

问题仅当我在文本区域中写得很快时才会出现。如果我写得慢的话,它就不会出现。我的猜测是,在我的打字之间,验证没有时间进行验证,默认情况下,它会显示错误?

无论如何,我可以改变这种行为吗?

I got the following JQuery validation script :

<script type="text/javascript">
$(document).ready(function() {
    $("#myForm").validate({
        rules: {
            "Content": {
                required: true,
                rangelength: [20, 1000],
                remote: {
                    url: "/RemoteValidation/IsValidContent",
                    timeout: 2000,
                    type: "post"
                }
            }
        },
        messages: {
            "Content": {
                required: "* ...A",
                rangelength: "* ...B",
                remote: "* ...C"
            }
        }
    });
});

And the following controller :

public class RemoteValidationController : Controller
    {
        [HttpPost]
        public virtual JsonResult IsValidContent(MyObject object)
        {    
            return new JsonResult
            {
                Data = true
            };
        }
}

It's only for test purpose, its always return true.

The problem I got is the following. I see the error message (...C) that appears for 1 second and that disapear...

I don't know how to solve that...

Why it appears... it's should never appear...

UPDATE

The problem only appear if I write really quickly in the textarea. If I write slowly, it's doesn't appear. My guess is that between my typing, the validation haven't no time to validated, and by default, it's show the error ?

Anyway, I can change this behavior ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜嗑 2024-09-26 23:21:02

您可以在键入时禁用验证,但只能在提交表单之前禁用:

$('#myForm').validate({
    onfocusout: false,
    onkeyup: false,
    ...
});

You could disable validation when typing but only before submitting the form:

$('#myForm').validate({
    onfocusout: false,
    onkeyup: false,
    ...
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文