繁体   English   中英

将这两个功能合而为一

[英]Combining these two functions into one

大家好,我有一个密码验证程序,我遇到的问题很严重,它很长,我认为可以缩短并简化它。

有人可以帮助我简化它。 我在谈论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,})$/)) {

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

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;
}

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

我设法弄清楚了,我将它们彼此组合在一起,只是将它们彼此组合在一起。

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM