Javascript密码长复杂验证

时间:2017-09-15 20:18:31

标签: javascript passwords

我有一个javascript代码,只能验证密码的整数。我想要的只是为“var exp”开发代码,密码必须是大写字符,小写字符,数字字符和特殊字符的组合。

HTML代码完美运行..

//Empty Validation//
function isEmpty(elemValue, field) {
  if (elemValue == "" || elemValue == null) {
    alert(field + " must not be empty!");
    return true;
  } else {
    return false;
  }
}

//empty validation end//

//password validation//          
function pwValidation(elemValue) {
  if (!isEmpty(elemValue, "Password")) {
    if (elemValue.length >= 8) {
      var exp1 = /^[0-9]+$/;

      if (elemValue.match(exp1)) {
        return true;
      } else {
        alert("Password must contain Upper, lower case, number and at least a special character");
        return false;
      }
    } else {
      alert("Password must at least 8 characters!");
    }
  } else {
    return false;
  }
}
//password validation end//

1 个答案:

答案 0 :(得分:0)

查看有关密码验证的上一个问题的this回答。如上所述,更改regEx以查找无效密码而不是有效密码更容易,因此您应该翻转逻辑,如下所示:



//Empty Validation//
function isEmpty(elemValue, field) {
  if (elemValue == "" || elemValue == null) {
    alert(field + " must not be empty!");
    return true;
  } else {
    return false;
  }
}

//empty validation end//


//password validation//          
function pwValidation(elemValue) {
  if (!isEmpty(elemValue, "Password")) {
    if (elemValue.length >= 8) {
      var exp1 = /^(.{0,7}|[^0-9]*|[^A-Z]*|[^a-z]*|[a-zA-Z0-9]*)$/;

      if (elemValue.match(exp1)) {
        alert("Password must contain Upper, lower case, number and at least a special character");
        return false;
      } else {
        return true;
      }
    } else {
      alert("Password must at least 8 characters!");
    }
  } else {
    return false;
  }
}
//password validation end//