jquery.validate pwcheck密码不起作用

时间:2017-08-08 03:12:56

标签: jquery regex jquery-validate

我使用了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');
      }
    }
  });
});

1 个答案:

答案 0 :(得分:0)

你应该用这个:

在顶部而不是底部添加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
});