Javascript 使用 jQuery 验证插件匹配两个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5147438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:50:32  来源:igfitidea点击:

Match two fields with jQuery validate plugin

javascriptjqueryasp.netjquery-validate

提问by Steven

I have an e-mail field, and a confirm e-mail field. I need to validate both of them to make sure their values match.

我有一个电子邮件字段和一个确认电子邮件字段。我需要验证它们以确保它们的值匹配。

Is there a way to add a rule to match those two fields?

有没有办法添加规则来匹配这两个字段?

回答by Darin Dimitrov

You could use the equalTomethod:

您可以使用equalTo方法:

$('#myform').validate({
    rules: {
        email: 'required',
        emailConfirm: {
            equalTo: '#email'
        }
    }
});

回答by Jazzy

You can also do this on the second field:

您也可以在第二个字段上执行此操作:

 class="required email" equalTo='#email'

回答by Naresh Kumar

U can use this

你可以用这个

email: {
      required: true, email: true
      },
c_email: { 
                required: true, equalTo: "#email", minlength: 5
          },

<div class="form-row"><span class="label">email</span><input type="email" name="email" class="required" id="email" /></div>

<div class="form-row"><span class="label">Confirm email</span><input type="email" name="c_email" id="c_email" /></div>