Password Validator
23 October 2024 at 18:17
import java.util.Scanner; public class PasswordValidator { public static void main(String[] args) { // PASSWORD MUST CONTAIN MIN 8 CHARS, MAX 12 CHARS, ATLEAST 1 UPPERCASE, 1 LOWERCASE, 1 DIGIT, 1 SPL CHAR Scanner scn = new Scanner(System.in); int maxLen = 12, minLen=8; System.out.println("Enter a password to validate"); String pwd = scn.next(); scn.close(); int n = pwd.length(); if (n<minLen || n>maxLen) { System.out.println("Password length must be between 8 to 12 characters"); return; } char[] pw = pwd.toCharArray(); int cntLower=0, cntUpper=0, cntDigit=0, cntSpl=0; for (int i=0; i<n; i++) { char ch = pw[i]; if (ch>='A' && ch<='Z') cntUpper++; else if (ch>='a' && ch<='z') cntLower++; else if (ch>='1' && ch<='9') cntDigit++; else if (ch=='!' || ch=='Β£' || ch=='$' || ch=='%' || ch=='^' || ch=='&' || ch=='*' || ch=='-' || ch=='_' || ch=='+' || ch=='=' || ch==':' || ch==';' || ch=='@'|| ch=='#'|| ch=='~'|| ch=='>'|| ch=='<'|| ch=='?'|| ch=='.') cntSpl++; } if (cntLower>0 && cntUpper>0 && cntDigit>0 && cntSpl>0) System.out.println("Password is valid..."); else { System.out.println("Password is NOT VALID"); System.out.println("PASSWORD MUST CONTAIN ATLEAST 1 UPPERCASE, 1 LOWERCASE, 1 DIGIT AND 1 SPL CHAR FROM ! Β£ $ % ^ & * - _ + = : ; @ ~ # ?"); } } }