【问题标题】:Combining these two functions into one将这两个功能合二为一
【发布时间】:2015-01-23 06:12:19
【问题描述】:

大家好,我有一个密码验证器,我正在处理它的问题,它很长,我认为如果可能的话可以缩短和简化。

有人可以帮助我简化它。我说的是 checkValidPassword() 函数。

function check(input) {
    if (input.value != document.getElementById('password').value) {
        input.setCustomValidity('Password Must be Matching.');
    } else {
        // input is valid -- reset the error message
        input.setCustomValidity('');
        // check the length of the password
        checkValidPassword(input);
    }
}


function checkValidPassword(input) {
    var password = document.getElementById('password');
    var confirm_password = document.getElementById('confirm password');
    if (password.value.length < 8) {
        password.setCustomValidity('Password must contain at least 8 characters!');
    } else {
        var re = /[0-9]/;
        if (!re.test(password.value)) {
            password.setCustomValidity('password must contain at least one number (0-9)!');
        } else {
            password.setCustomValidity("");
        }
    }
}

我试图实现一种方式,让用户也必须至少包含一个数字。我在想

str.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8,})$/)

我是否可以在 if 语句中使用 $$ 来表示 并检查字符 吗?

if(password.value.length < 8 && str.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8,})$/)) {

【问题讨论】:

    标签: javascript validation passwords


    【解决方案1】:

    我设法弄明白了,我将它们两者结合在一起,只需将 else 放在一起。

    function ValidatePassword(pass, confirm_pass) {
        if (pass.value != confirm_pass.value || pass.value == "" || confirm_pass.value == "") {
            confirm_pass.setCustomValidity("the Passwords do not match");
            pass.setCustomValidity("the Passwords do not match");
        } else {
          if(pass.value.match(/(?=^.{8,30}$)([a-zA-Z]+[0-9])$/)) {
    
                pass.setCustomValidity("");
                confirm_pass.setCustomValidity("");
    
    
            } else {
                 pass.setCustomValidity("the password doesnt have numbers");
                confirm_pass.setCustomValidity("the password doesnt have numbers");
            }
        }
    }
    

    这是我制作的表单的样子:

    <form>
    
            password
            <input id="pass" type="password" required="" placeholder="Password" />
            <br> confirm
            <input id="confirm_pass" type="password" required="" placeholder="confirm" onfocus="ValidatePassword(document.getElementById('pass'), this);" oninput="ValidatePassword(document.getElementById('pass'), this);" />
            <br> username :
            <input id="username" required="" type="text">
            <br>
            <button class="btnform" name="register" type="submit">Complete Registration</button>
    </form>
    

    【讨论】:

      【解决方案2】:

      这本质上是一个代码审查问题,但是好的...我会将您的函数重写为:

      function checkPassword() {
      
          var password = document.getElementById('password');
          var confirm_password = document.getElementById('confirm password');
      
          if (password.value != confirm_password.value) {
              password.setCustomValidity('Password Must be Matching.');
              return false;
          }
      
          if(password.value.length < 8 ) {
              password.setCustomValidity('Password must contain at least 8 characters!');
              return false;
          }
      
          if(!/[0-9]/.test(password.value)) {
              password.setCustomValidity('password must contain at least one number (0-9)!');
              return false;
          }
      
          return true;
      }
      

      基本上,单独检查每个条件并在失败时立即返回,从而避免额外的缩进(“提前退出”)。这有点冗长,但比怪物正则表达式更具可读性,尤其是在您不确定它的作用时。

      【讨论】:

      • 感谢@georg,目前我正在使用 我会保留那个 oninput(checkthis) 功能吗?或将其更改为 checkPassword()
      • 只要checkPassword 就可以了。
      • 感谢您的帮助,但它似乎在这里不起作用是一个 jsf:jsfiddle.net/xb4u0453 我说得对。
      • @xtrman:您需要使用“不换行”选项并仔细检查所有 ID 是否匹配。
      • @xtrman:这只是一个例子。我不知道你的其余代码是什么样子的。
      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-07
      • 2023-03-10
      • 2016-07-27
      • 1970-01-01
      相关资源
      最近更新 更多