【问题标题】:jquery.validate pwcheck password not workjquery.validate pwcheck 密码不起作用
【发布时间】:2017-08-08 03:12:55
【问题描述】:

我用了jquery.validate,我想在用户输入时验证密码但它不起作用。

检查 pwcheck 密码不起作用。请看看我怎么了?

if (jQuery().validate) {
  $.validator.addMethod("pwcheck", function(value) {
    return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) // consists of only these
      &&
      /[a-z]/.test(value) // has a lowercase letter
      &&
      /\d/.test(value) // has a digit
  });

  $('#myform').validate({
    rules: {
      forename: {
        required: true,
        maxlength: 300
      },
      surname: {
        required: true,
        maxlength: 300
      },
      password: {
        required: true,
        minlength: 8,
        maxlength: 300
      },
      password_repeat: {
        required: true,
        minlength: 8,
        maxlength: 300,
        equalTo: '#password'
      }
    },
    messages: {
      password: {
        pwcheck: "Das Passwort entspricht nicht den Kriterien!",
      }
    },
    submitHandler: function(form) {
      if (grecaptcha.getResponse()) {
        form.submit();
      } else {
        alert('Please complete the reCaptcha to proceed');
      }
    }
  });
});

【问题讨论】:

  • 您创建了名为pwcheck 的新规则,但您没有在任何地方使用它。
  • 我解决了我的问题。原因是因为我没有添加pwcheck: true

标签: jquery regex jquery-validate


【解决方案1】:

你应该使用这个:

在顶部而不是底部添加equalTo: "#password",。

password: {
   required: true,
   pwcheck: true,
   minlength: 8,
   maxlength: 300
},
password_repeat: {
   equalTo: "#password",
   required: true,
   minlength: 8,
   maxlength: 300,
}

在此处添加:

/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/

但是,如果没有前瞻,这样做会容易得多:

$.validator.addMethod("pwcheck", function(value) {
   return /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/.test(value) // consists of only these
       && /[a-z]/.test(value) // has a lowercase letter
       && /\d/.test(value) // has a digit
});

【讨论】:

  • 输入时要验证密码,用户必须输入包含小写字母、大写字母、特殊字符、长度>8
  • 它有效,但我只输入了8个字符并添加了1个小写字母是否有效?我想输入时验证密码,用户必须输入包含小写字母,大写字母,特殊字符,长度>8
  • 使用这个(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/)
  • OK,但是我希望用户输入的密码中必须包含特殊字符(例如:@#$%^!&*),我的正则表达式不好:)
  • 使用这个:^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$ 并在此处验证:regex101.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 2015-02-24
  • 2015-12-19
  • 1970-01-01
相关资源
最近更新 更多