简体   繁体   English

验证Javascript中的密码匹配

[英]Validating password Match in Javascript

I am trying to validate both passwords field and checking that both of them match but the form still gives an error even if both input boxes have the same password. 我正在尝试验证两个密码字段,并检查它们是否都匹配,但是即使两个输入框具有相同的密码,该表格仍然会给出错误。

JS JS

var validator = $("#signupform").validate({
  rules: {
    password: {
      required: true,
      minlength: 6
    },

    repeatpassword: {
      required: true,
      minlength: 6,
      equalTo: "#password"
    }

  },
  messages: {
    password: {
      required: "Provide a password",
      minlength: jQuery.format("Enter at least {0} characters")
    },
    repeatpassword: {
      required: "Repeat your password",
      minlength: jQuery.format("Enter at least {0} characters"),
      equalTo: "Your passwords do not match"
    }
  },
});

HTML HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-xs-12 col-sm-6 col-md-6">
  <div class="form-group">
    <input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="5">
  </div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
  <div class="form-group">
    <input type="password" name="repeatpassword" id="repeatpassword" class="form-control input-lg" placeholder="Confirm Password" tabindex="6">
  </div>
</div>

Not sure if you knew this but, "It is absolutely required that you have <form></form> tags for the jQuery Validate plugin to function properly, or at all." 不知道您是否知道这一点,但是,“绝对需要您具有<form></form>标记,才能使jQuery Validate插件正常运行,或者完全正常运行。” Read More 阅读更多

So I added a form tag. 所以我添加了一个表单标签。

What I think the issue with the passwords was, your were doing this: 我认为密码的问题是,您正在这样做:

jQuery.format("Enter at least {0} characters")

There is an error in console: 控制台中存在错误:

"$.format has been deprecated. Please use $.validator.format instead."; “;已弃用$ .format。请改用$ .validator.format。

So I changed your code to: 因此,我将您的代码更改为:

jQuery. jQuery的。 validator .format("Enter at least {0} characters") 验证程序 .format(“至少输入{0}个字符”)

 var validator = $("#signupform").validate({ rules: { password: { required: true, minlength: 6 }, repeatpassword: { required: true, minlength: 6, equalTo: "#password" } }, messages: { password: { required: "Provide a password", minlength: jQuery.validator.format("Enter at least {0} characters") }, repeatpassword: { required: "Repeat your password", minlength: jQuery.validator.format("Enter at least {0} characters"), equalTo: "Your passwords do not match" } }, }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script> <form id="signupform"> <div class="col-xs-12 col-sm-6 col-md-6"> <div class="form-group"> <input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="5"> </div> </div> <div class="col-xs-12 col-sm-6 col-md-6"> <div class="form-group"> <input type="password" name="repeatpassword" id="repeatpassword" class="form-control input-lg" placeholder="Confirm Password" tabindex="6"> </div> </div> <input type="submit" value="Submit"> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM