Home > Mobile >  Password Validator using loops and objects
Password Validator using loops and objects

Time:11-12

I'm trying to validate a password to see if meets the following criteria:

  • Must have at least one uppercase character
  • Must have at least one lowercase character
  • Must have at least one numerical character If a certain condition is not met, the function must throw an error, word-for-word, that has been specified in the criteria.

I tried using multiple if-statements because that way you can isolate the condition and throw the exact error, but I'm trying to find a way to do it using objects and loops. Also, doing this following the TDD methodology with Jasmine as my testing framework. All my tests expect an error message and I'm having trouble returning the appropriate error messages.

Here is my code so far:

const regexObj = {
    lowercaseRegex: {
        regex: /[a-z]/,
        errorMsg: "password should have at least one lowercase letter",
    },
    uppercaseRegex: {
        regex: /[A-Z]/,
        errorMsg: "password should have at least one uppercase letter",
    },
    oneDigitRegex: {
        regex: /[0-9]/,
        errorMsg: "password should have at least one digit",
    },
    specialCharsRegex: {
        regex: /[^A-Za-z0-9]/,
        errorMsg: "password should have at least one special character",
    },
    whitespaceRegex: {
        regex: /\s/,
        errorMsg: "password should have at least one whitespace character",
    },
};

function checkPassword(password){
  let errorMsgs = [];
for(let x = 0; x < password.length; x  ) {
    for(let prop in regexObj){
        if(!regexObj[prop].regex.test(password[x])){
            errorMsgs.push(regexObj[prop].errorMsg);
        }
    }
  }
    return (errorMsgs || true);
}

CodePudding user response:

You seem to be testing various regular expressions against each character of the password. You should instead be testing each regular expression against the entire password. Why? If you require a lower-case letter, for example, you shouldn't care where in the password that character exists. As long as it can be found anywhere in the password it's okay.

Also, as it now stands a whitespace character would be considered a special character because it is not alphanumeric. Thus the regex for special characters should also not be searching for a whitespace character.

Finally, your return statement will never return true as it is currently written since errorMsgs will always evaluate as true.

const regexObj = {
    lowercaseRegex: {
        regex: /[a-z]/,
        errorMsg: "password should have at least one lowercase letter",
    },
    uppercaseRegex: {
        regex: /[A-Z]/,
        errorMsg: "password should have at least one uppercase letter",
    },
    oneDigitRegex: {
        regex: /[0-9]/,
        errorMsg: "password should have at least one digit",
    },
    specialCharsRegex: {
        regex: /[^A-Za-z0-9\s]/,
        errorMsg: "password should have at least one special character",
    },
    whitespaceRegex: {
        regex: /\s/,
        errorMsg: "password should have at least one whitespace character",
    },
};

function checkPassword(password) {
    let errorMsgs = [];
    for (let prop in regexObj) {
        if(!regexObj[prop].regex.test(password)) {
            errorMsgs.push(regexObj[prop].errorMsg);
        }
    }
    return errorMsgs.length ? errorMsgs : true;
}

console.log(checkPassword('ab C'));

Update

If you want to throw an exception instead of returning an array of errors, then:

function checkPassword(password) {
    const regexObj = {
        lowercaseRegex: {
            regex: /[a-z]/,
            errorMsg: "password should have at least one lowercase letter",
        },
        uppercaseRegex: {
            regex: /[A-Z]/,
            errorMsg: "password should have at least one uppercase letter",
        },
        oneDigitRegex: {
            regex: /[0-9]/,
            errorMsg: "password should have at least one digit",
        },
        specialCharsRegex: {
            regex: /[^A-Za-z0-9\s]/,
            errorMsg: "password should have at least one special character",
        },
        whitespaceRegex: {
            regex: /\s/,
            errorMsg: "password should have at least one whitespace character",
        },
    };

    for (let prop in regexObj) {
        if(!regexObj[prop].regex.test(password)) {
            throw regexObj[prop].errorMsg;
        }
    }
    return true;
}

console.log(checkPassword('ab C'));

  • Related