阿贾克斯验证

发布于 2025-01-06 06:50:29 字数 807 浏览 0 评论 0原文

我希望使用 OnBlur 事件(以覆盖按键以及剪切和粘贴事件)来使用 ajax 验证来验证表单元素。看起来jquery Validation 1.9确实有这个功能。任何人都可以确认它除了复制/粘贴条目之外还涵盖了键入的条目吗?有使用 Asp.net MVC2 的示例吗?我没有强类型视图,因此我的实现可能会比通常的示例稍微复杂一些...

到目前为止,我将尝试调用这个函数...

public string ValidateHosFin(string hospitalFin)
{
    if (!true)
        return "";
    return "true";

}

这只是该函数真正用途的一个虚拟正在做...但这只是一个测试...到目前为止,我的 jquery 验证函数设置如下...

$("#temp1").validate({
        rules: {
            HospitalFinNumber: {
                required: true,
                minlength: 6,
                remote: { url: '<%:Url.Action("ValidateHosFin", "PatientACO")%>', data: { hospitalFin: $('#HospitalFinNumber').val() }
                }
            }
}

我将验证的输入元素 id 是 HospitalFinNumber。 我如何确保这适用于按键输入事件以及复制粘贴事件?

I am looking to validate a form element using ajax validation using the OnBlur event (to cover key ups and cut and paste events). It looks like jquery Validation 1.9 does indeed have this feature. Can anyone confirm that it covers both key'ed entries in addition to copy/paste entries? Any examples of this using Asp.net MVC2? I do not have a strongly type view so my implementation may end being slightly more complicated that the usual example...

So far, I am going to try and call this function...

public string ValidateHosFin(string hospitalFin)
{
    if (!true)
        return "";
    return "true";

}

This is just a dummy for what the function really will be doing... That is just a test however... So far, I have my jquery Validation function set up like so...

$("#temp1").validate({
        rules: {
            HospitalFinNumber: {
                required: true,
                minlength: 6,
                remote: { url: '<%:Url.Action("ValidateHosFin", "PatientACO")%>', data: { hospitalFin: $('#HospitalFinNumber').val() }
                }
            }
}

The input element id for what I will be validating is HospitalFinNumber.
How can I ensure that this will work on both key input event's as well as copy paste events?

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

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

发布评论

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

评论(1

∞梦里开花 2025-01-13 06:50:29

是的,这将涵盖 onBlur 事件和复制/粘贴到字段中。另外,您可能想要修改服务器端验证操作,以便它返回 ActionResult 和 JSON:

public ActionResult ValidateHosFin(string hospitalFin)
{
    // some validation logic
    if (hospitalFin == "1234567") 
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }
    return Json("invalid hospital fin", JsonRequestBehavior.AllowGet);
}

另一个注释是您的远程验证规则。 值将是显示页面时文本框的值(可能是空的):

$('form').validate({
    rules: {
        HospitalFinNumber: {
            required: true,
            minlength: 6,
            remote: function() {
                return {
                    url: '<%= Url.Action("ValidateHosFin", "PatientACO") %>',
                    data: { hospitalFin: $('#HospitalFinNumber').val() }
                };
            }
        }
    }
});

您应该让它返回一个函数,否则发送到服务器的 net/K87vL/9/" rel="nofollow">现场演示。

Yes, this will cover both onBlur events and copy/paste into the field. Also you might want to modify your server side validation action so that it returns an ActionResult and JSON:

public ActionResult ValidateHosFin(string hospitalFin)
{
    // some validation logic
    if (hospitalFin == "1234567") 
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }
    return Json("invalid hospital fin", JsonRequestBehavior.AllowGet);
}

Another remark is your remote validation rule. You should make it return a function or the value sent to the server will be the value that the textbox had at the moment the page was shown (which is probably empty):

$('form').validate({
    rules: {
        HospitalFinNumber: {
            required: true,
            minlength: 6,
            remote: function() {
                return {
                    url: '<%= Url.Action("ValidateHosFin", "PatientACO") %>',
                    data: { hospitalFin: $('#HospitalFinNumber').val() }
                };
            }
        }
    }
});

And here's a live demo.

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