【问题标题】:Javascript Password Long complex ValidationJavascript密码长复杂验证
【发布时间】:2017-09-15 20:18:31
【问题描述】:

我有一个 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//

【问题讨论】:

    标签: javascript passwords


    【解决方案1】:

    查看this 对先前有关密码验证的问题的回答。如前所述,更改正则表达式以查找无效密码比查找有效密码更容易,因此您应该翻转逻辑,如下所示:

    //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//

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-19
      • 2014-05-21
      • 2011-08-14
      • 2013-12-09
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多