Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function passVall(password) {
- // Write a function that checks if a given password is valid. Password validations are:
- // • The length should be 6 - 10 characters (inclusive)
- function length(password) {
- let counter = 0;
- for (let index = 0; index < password.length; index++) {
- counter++
- }
- if (password.length >= 6 && password.length <= 10) {
- return true
- } else {
- console.log(`Password must be between 6 and 10 characters`)
- return false
- }
- }
- function charCheck(password) {
- for (let index = 0; index < password.length; index++) {
- let temp = password[index];
- let tempChar = temp.charCodeAt(0);
- if ((tempChar >= 97 && tempChar <= 122) || (tempChar >= 65 && tempChar <= 90)
- ||(tempChar >= 48 && tempChar <= 57)) {
- } else {
- console.log(`Password must consist only of letters and digits`);
- return false
- }
- }
- return true
- }
- function digitCheck(password) {
- let digitCount = 0;
- for (let index = 0; index < password.length; index++) {
- let temp = password[index];
- let tempChar = temp.charCodeAt(0);
- if (tempChar >= 48 && tempChar <= 57) {
- temp = Number(temp);
- digitCount++
- }
- }
- if (digitCount >= 2) {
- return true
- } else {
- console.log("Password must have at least 2 digits");
- return false
- }
- }
- let lengthCheck = length(password);
- let char = charCheck(password);
- let digitsCheck = digitCheck(password);
- if (lengthCheck === true && digitsCheck === true && char===true) {
- console.log("Password is valid");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement