Javascript密码匹配和复杂性问题

时间:2015-02-13 12:14:40

标签: javascript regex validation passwords

这是我的脚本代码

function validateForm() {
    var u = document.forms["myForm"]["uname"].value;
    var x = document.forms["myForm"]["password1"].value;
    var xx = document.forms["myForm"]["password2"].value;
    if (u=="" || ( x != /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/ || (x != xx || x=="" ))) {
        alert("Passwords do not match or username is blank. Passwords must be a minimum of 8 characters and contain at least 1 UPPER case 1 Lower case and 1 number");
        return false;
    }
}

这是完整的代码

<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
    var u = document.forms["myForm"]["uname"].value;
    var x = document.forms["myForm"]["password1"].value;
    var xx = document.forms["myForm"]["password2"].value;
    if (u=="" || ( x != (?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20} || (x != xx || x=="" ))) {
        alert("Passwords do not match or username is blank. Passwords must be a minimum of 8 characters and contain at least 1 UPPER case 1 Lower case and 1 number");
        return false;
    }
}
</script>
</head>
<body>
<H1>Crypt for htpasswd file</H1><br>

<form name="myForm" action="htpasswd.php" onsubmit="return validateForm()" method="post">
Username:<br> <input TYPE="text" NAME="uname"><br>
Password:<br> <input TYPE="password" NAME="password1"><br>
Password:<br> <input TYPE="password" NAME="password2">

<input type="submit" value="Submit">
</form>

</body>
</html>

无论发生什么事,都会提示密码不正确。

它与脚本中的正则表达式有关,我无法想象why.adsad

4 个答案:

答案 0 :(得分:0)

您无法使用简单的相等/不等运算符来测试正则表达式匹配。您可以使用 RegExp.test()

if (u === "" || 
    !/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}/.test(x) || 
    x !== xx) {
    ...
}

演示:http://jsfiddle.net/w0svjzd2/ [表格提交已被故意禁用小提琴演示]

答案 1 :(得分:0)

这不是您使用regex的正确方法:

   function validateForm() {
      var u = document.forms["myForm"]["uname"].value;
      var x = document.forms["myForm"]["password1"].value;
      var xx = document.forms["myForm"]["password2"].value;

      // Create a pattern obj
      var patt = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;

      if (u=="" || ( patt.test(x) || (x != xx || x=="" ))) {
         alert("Passwords do not match or username is blank. Passwords must be a minimum of 8 characters and contain at least 1 UPPER case 1 Lower case and 1 number");
        return false;
      }
   }

答案 2 :(得分:0)

使用test()方法检查字符串是否与正则表达式匹配 像这样 -

>  x="1aA123"
>  /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/.test(x)
>> true

答案 3 :(得分:0)

试试这个......

<script type="text/javascript">

  function checkForm(form)
  {
    if(form.username.value == "") {
      alert("Error: Username cannot be blank!");
      form.username.focus();
      return false;
    }
    re = /^\w+$/;
    if(!re.test(form.username.value)) {
      alert("Error: Username must contain only letters, numbers and underscores!");
      form.username.focus();
      return false;
    }

    if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
      if(form.pwd1.value.length < 6) {
        alert("Error: Password must contain at least six characters!");
        form.pwd1.focus();
        return false;
      }
      if(form.pwd1.value == form.username.value) {
        alert("Error: Password must be different from Username!");
        form.pwd1.focus();
        return false;
      }
      re = /[0-9]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one number (0-9)!");
        form.pwd1.focus();
        return false;
      }
      re = /[a-z]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one lowercase letter (a-z)!");
        form.pwd1.focus();
        return false;
      }
      re = /[A-Z]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one uppercase letter (A-Z)!");
        form.pwd1.focus();
        return false;
      }
    } else {
      alert("Error: Please check that you've entered and confirmed your password!");
      form.pwd1.focus();
      return false;
    }

    alert("You entered a valid password: " + form.pwd1.value);
    return true;
  }

</script>

<form ... onsubmit="return checkForm(this);">
<p>Username: <input type="text" name="username"></p>
<p>Password: <input type="password" name="pwd1"></p>
<p>Confirm Password: <input type="password" name="pwd2"></p>
<p><input type="submit"></p>
</form>