Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function passwordValidator(input) {
- let password = input;
- let isValid = true;
- if (password.length < 6 || password.length > 10) {
- isValid = false;
- console.log('Password must be between 6 and 10 characters');
- }
- for (let ch of password) {
- if (!(ch.charCodeAt(0) >= 48 && ch.charCodeAt(0) <= 57) && (ch.charCodeAt(0) >= 65 && ch.charCodeAt(0) <= 90) && (ch.charCodeAt(0) >= 97 && ch.charCodeAt(0) <= 122)) {
- isValid = false;
- console.log('Password must consist only of letters and digits');
- break;
- }
- }
- let digits = 0;
- for (let i = 0; i < password.length; i++) {
- let currentChar = password[i];
- if (currentChar.charCodeAt(i) >= 48 && currentChar.charCodeAt(i) <= 57) {
- digits++;
- if (digits < 2) {
- isValid = false;
- console.log('Password must have at least 2 digits');
- }
- }
- }
- if (isValid) {
- console.log('Password is valid');
- }
- }
- passwordValidator('MyPass123');
- passwordValidator('logIn');
- passwordValidator('Pa$s$s');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement