Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function validatePassword(pass) {
- if (isLengthCorrect(pass) && isOnlyLettersAndDigits(pass) && isAtLeastTwoDigits(pass)) {
- console.log(`Password is valid`);
- }
- if (!isLengthCorrect(pass)) {
- console.log('Password must be between 6 and 10 characters');
- }
- if (!isOnlyLettersAndDigits(pass)) {
- console.log('Password must consist only of letters and digits');
- }
- if (!isAtLeastTwoDigits(pass)) {
- console.log('Password must have at least 2 digits');
- }
- function isLengthCorrect(text) {
- return text.length >= 6 && text.length <= 10
- }
- function isOnlyLettersAndDigits(text) {
- let isValid = true;
- for (let i = 0; i < text.length; i++) {
- let currentChar = text[i];
- if (isNaN(+currentChar) && currentChar.toLowerCase() === currentChar.toUpperCase()) {
- isValid = false;
- break;
- }
- }
- return isValid;
- }
- function isAtLeastTwoDigits(text) {
- let digitsCounter = 0
- for (let i = 0; i < text.length; i++) {
- if (!isNaN(+text[i])) {
- digitsCounter++;
- }
- }
- return digitsCounter >= 2
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement