Home > database >  How can I check for special characters in password validation in Java?
How can I check for special characters in password validation in Java?

Time:04-05

I am trying to get the code to check for special characters entered, however, I am not getting any validation errors when special characters are not entered in the password. Is there a reason why special characters are not been identified? All the other password validation checks work fine

//package sampleProject;
import java.util.*;
import java.util.Scanner;

public class Lab7 {

      public static void main(String []args){
         
             // Specify the maximum number of letters in a password
             final int MAX=10;
             int invalidCount = 0;                          
         
             // Specifying the number of uppercase letters in password
             final int MIN_Uppercase=1;
             // Specifying the minimum lowercase letters in password
             final int NUM_Digits=2;
             // Specify the minimum number of special case letters
             final int Special=1;
             // Count number of uppercase letters in a password
             int uppercaseCounter=0;
             // Count digits in a password
             int digitCounter=0;
             // count special case letters in a password
             int specialCounter=0;            
                    
 System.out.println("Enter the password\n");
            
 Scanner input = new Scanner(System.in);        
            
 String password = input.nextLine();
             
             for (int i=0; i < password.length(); i   ) {
                    char c = password.charAt(i);
                    if(Character.isUpperCase(c)) 
                          uppercaseCounter  ;
                    else if(Character.isDigit(c)) 
                          digitCounter  ;     
                     if(c == '$' || c == '#' || c == '?' || c == '!' || c == '_' || c == '=' || c == '%'){
                      specialCounter  ;
                 }                    
             }
             
             if (password.length() >= MAX && uppercaseCounter >= MIN_Uppercase && specialCounter == 1 && digitCounter == 2 || digitCounter == 3) { 
                    System.out.println("Valid Password");
             }
             else {
                    System.out.println("Your password does not contain the following:");                    
                    
                    if(password.length() < MAX)
                          System.out.println("Enter atleast 10 characters");
                    if (uppercaseCounter < MIN_Uppercase) 
                          System.out.println("Enter at least 1 uppercase character");
                    if(digitCounter != 2 || digitCounter != 3) 
                          System.out.println("Enter either 2 or 3 digits");
                    if(specialCounter > 1)
                          System.out.println("Password should contain only 1 special characters");                    
             }            
    }
}

CodePudding user response:

You don't get any validaton errors when special characters are not entered because this is not what your check does:

if(specialCounter > 1)
    System.out.println("Password should contain only 1 special characters");

You are printing an error message when there are more than one special characters (which doesn't make sense). I suggest:

if(specialCounter < 1)
    System.out.println("Password should contain at least 1 special characters");                    
                

CodePudding user response:

What you should do this instead in this line :

if(specialCounter != 1)
    System.out.println("Password should contain only 1 special characters");

if it is not equal to 0, it will throw the exception. The != means not equal, so if it is less or above 1, It will do the error.

  • Related