使用 jQuery Validate 验证两个字段

发布于 2024-11-07 21:00:03 字数 532 浏览 1 评论 0原文

我正在使用 jQuery 验证插件: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

简单演示:

  <input type="text" name="email" class="required email" />
  <input type="text" name="email_confirm" class="required email" />

如何使用 jQuery 或 jQuery 验证插件检查两个输入中的值是否相同。

我刚刚尝试了 equalto 并且它有效:

rules: {
  email: 'required',
  email2: {equalTo: '.email'}

I'm using jQuery Validate Plugin:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Simple demo:

  <input type="text" name="email" class="required email" />
  <input type="text" name="email_confirm" class="required email" />

How to check if the value in the two inputs are the same with jQuery or with jQuery Validate Plugin.

I just tried equalto and it works:

rules: {
  email: 'required',
  email2: {equalTo: '.email'}

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

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

发布评论

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

评论(1

两人的回忆 2024-11-14 21:00:03

通过查看演示页面 http://jquery.bassistance.de/validate/demo/ 您可以使用“equalTo”验证两个应该相等的字段:

    $("#signupForm").validate({
    rules: {
                    ...snip... 
        password: {
            required: true,
            minlength: 5
        },
        confirm_password: {
            required: true,
            minlength: 5,
            equalTo: "#password"
        },
                   ...snip...
    },
    messages: {
        ...snip...
        password: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long"
        },
        confirm_password: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long",
            equalTo: "Please enter the same password as above"
        },
        ...snip...
    }
});

要使用类方法来执行此操作,您只需找到语法即可。我找不到 equalTo 属性,但它可能类似于:

class="required email equalTo['email']"

By looking at the demo page at http://jquery.bassistance.de/validate/demo/ you can validate 2 fields that should be equal by using "equalTo":

    $("#signupForm").validate({
    rules: {
                    ...snip... 
        password: {
            required: true,
            minlength: 5
        },
        confirm_password: {
            required: true,
            minlength: 5,
            equalTo: "#password"
        },
                   ...snip...
    },
    messages: {
        ...snip...
        password: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long"
        },
        confirm_password: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long",
            equalTo: "Please enter the same password as above"
        },
        ...snip...
    }
});

To do it using the class method, you just need to find the syntax. I couldn't find it for the equalTo attribute, but it is probably something like:

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