Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function passwordValidator(password) {
- let lengthValidation = hasRequiredLength(password);
- let conditionValidation = hasValidCharacters(password);
- let digitContainsValidation = hasAtleastTwoDigits(password);
- if (lengthValidation && conditionValidation && digitContainsValidation) {
- console.log('Password is valid')
- }
- function hasRequiredLength(password) {
- let isInRange = password.length >= 6 && password.length <= 10;
- if (!isInRange) {
- console.log('Password must be between 6 and 10 characters');
- }
- return isInRange;
- }
- function hasValidCharacters(password) {
- let characters = password.split('');
- let isValid = true;
- for (let letter of characters) {
- let assciiValue = letter.charCodeAt();
- if (!isDigit(assciiValue) && !isCapitalLetter(assciiValue) && !isLowerLetter(assciiValue)) {
- isValid = false;
- break;
- }
- }
- if (!isValid) {
- console.log('Password must consist only of letters and digits');
- }
- return isValid;
- function isDigit(char) {
- return char >= 48 && char <= 57;
- }
- function isCapitalLetter(char) {
- return char >= 65 && char <= 90;
- }
- function isLowerLetter(char) {
- return char >= 97 && char <= 122;
- }
- }
- function hasAtleastTwoDigits(password) {
- let digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
- let digitCounter = 0;
- password.split('').forEach((letter) => {
- let value = Number(letter);
- if (digits.includes(value)) {
- digitCounter++;
- }
- })
- let hasMinimumDigits = digitCounter >= 2;
- if (!hasMinimumDigits) {
- console.log("Password must have at least 2 digits");
- }
- return hasMinimumDigits
- }
- }
Add Comment
Please, Sign In to add comment