Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function passwordValidator(password) {
- let valLength = validLength(password);
- let valSymbols = checkValidSymbols(password);
- let valDigits = checkTwoDigits(password);
- printResult(valLength, valSymbols, valDigits);
- function validLength(password) {
- return password.length >= 6 && password.length <= 10;
- }
- function checkValidSymbols(text) {
- for (let sym of text) {
- let symbol = sym.charCodeAt(0);
- let digitsInPassword = checkForDigits(symbol);
- let letters = checkForLetters(symbol);
- if (!digitsInPassword && !letters) {
- return false;
- }
- }
- return true;
- }
- function checkForDigits(char) {
- return char >= 48 && char <= 57
- }
- function checkForLetters(char) {
- return (char >= 65 && char <= 90) || (char >= 97 && char <= 122)
- }
- function checkTwoDigits(password) {
- let digitCounter = 0;
- for (let sym of password) {
- let digitsInPassword = checkForDigits(sym.charCodeAt(0));
- if (digitsInPassword) {
- digitCounter++;
- }
- }
- return digitCounter >= 2
- }
- function printResult(valLength, valSymbols, valDigits) {
- if (!valLength) {
- console.log("Password must be between 6 and 10 characters");
- }
- if (!valSymbols) {
- console.log("Password must consist only of letters and digits");
- }
- if (!valDigits) {
- console.log("Password must have at least 2 digits");
- }
- if (valLength && valSymbols && valDigits) {
- console.log("Password is valid");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment