将这两个功能合二为一

时间:2014-11-25 09:46:48

标签: javascript validation passwords

嘿伙计们,我有一个密码验证器,我有问题,它很长,我想可以缩短和简化,如果可能的话。

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

2 个答案:

答案 0 :(得分:1)

这本质上是一个代码审查问题,但是好吧......我会将你的功能重写为:

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

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

答案 1 :(得分:1)

我设法解决了这个问题,我把两者结合在一起就把它们结合起来了。

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>
相关问题