JavaScript Password Validator

Write a program to validate password using regex expression


Youtube Video: https://youtu.be/piJsqtEQ8_Q


Problem Statement: 

Write a program that takes in a string as input and evaluates it as a valid password. The password is valid if it has at a minimum 2 numbers, 2 of the following special characters ('!', '@', '#', '$', '%', '&', '*'), and a length of at least 7 characters.

If the password passes the check, output 'Strong', else output 'Weak'.

Input Format:

A string representing the password to evaluate.

Output Format:

A string that says 'Strong' if the input meets the requirements, or 'Weak', if not.

Sample Input: 

Hello@$World19

Sample Output: 

Strong


passwordValidator.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Validator</title>
<script src="passwordValidator.js"></script>
</head>
<body>
<input id="password" />
<button onclick="validate()">Validate</button>
</body>
</html>


passwordValidator.js



function validate() {
// This problem can be solved using regex

// First get the value entered by the user in the input box

const inputValue = document.getElementById('password').value;
if (inputValue) {

// Let us define the regex which will validate the input
const regex = /(?=.*[0-9]{2,})(?=.*[!@#$%&*]{2,})[a-zA-Z0-9!@#$%&*]{7,}/;

// Here, (?=.*[0-9]{2,}) will check if the string entered has atleast 2 numbers
// (?=.*[!@#$%&*]{2,}) will check if the string entered has atleast 2 of the symbols defined in []
// and the last part will check all the entered characters are at least 7 length

// This type of regex is called lookahead assertions

if (regex.test(inputValue)) {
alert('Strong')
} else {
alert('Weak');
}
}
}




Comments

Popular Posts