Javascript password validator

Program:  Javascript program to check if a password fullfils the following condition:

1. Should be minimum 5 characters
2. Should be maximum 10 characters long
3. Should contain at least one symbol
4. Should contain at least one number
5. Should not contain any space

Solution: 


let password = "as4kjd$";
const symbolsRegex = /[|\/~^:,;?!&%$@*+]/;
if (password.length < 5) {
console.log("must be greater than 5");
} else if (password.length > 10) {
console.log("must be greater than 10");
} else if (password.indexOf(" ") >= 0) {
console.log("should not contain space");
} else if (!/\d/.test(password)) {
console.log("should contain a number.");
} else if (!symbolsRegex.test(password)) {
console.log("Should contain a symbol");
} else {
console.log(`${password} : is a valid password`);
}

Comments

Popular Posts