5+ Java Password Validator Programs

This post contains a total of 5+ Java Password Validator Program Examples with Source Code. All these programs to Validate Passwords are made using Java.

You can use the source code of these examples with credits to the original owner.

Related Posts

Java Password Validator Programs

1. By PapaBT

Made by PapaBT. Simple Password validator program. Source

pass
false
import java.util.Scanner;

public class Program
{
    public static void main(String[] args) {
        Scanner in =new Scanner (System.in);
        String s = in.nextLine();
        in.close();
        System.out.println(s);
        System.out.println(validate(s));
    }
    private static boolean validate 
        (String s) {
        if (s.length() < 5 || 
            s.length() > 10)
            return false;
        if (! s.matches (".*\\d.*"))
            return false;
        if (! s.matches (".*\\W.*"))
            return false;            
        if (s.matches (".*\\s.*"))
            return false;         
        return true;          
    }
}

2. By Niki

Made by Niki. Basic Java Password Validator. Source

Mypas123&& Valid password
import java.util.Scanner;
import java.util.regex.*;

public class Program
{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String password=input.nextLine();
        String rules="^(?=(.*?\\d))(?=(.*[[email protected],;:#$%&*]))(?!.*\s)[[email protected],;:#$%&*]{5,10}$";
        if(password.matches(rules))
            System.out.println(password+ " Valid password");
        else
            System.out.println(password+ " Invalid password.\n⚠Read the rules in the comment.⚠");
    
    }
}

3. By Nimisha Agarwal

Made by Nimisha Agarwal. Source

Qwer
not valid
import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
        
        String a;
        int n=0,sym=0,sp=0;
        Scanner sc= new Scanner(System.in);
        a=sc.nextLine();
        int l=a.length();
        if(l>=5&&l<=10)
        {
         for(int i=0;i<l;i++)
        {
           char su=a.charAt(i);
           int s=(char)(su);
           
           if(s>=48&&s<=57)
           n++;
           
          if((s >= 33 && s<= 47) || (s>= 58 && s<= 64) || (s >= 91 && s <= 96) || (s>= 123 && s <= 127))
          sym++;
          if(s==32)
          sp++;
         }}
        if(n==0||sym==0||sp!=0)
        
        System.out.println("not valid");
        else
        
        System.out.println("valid");
    
    }
}

4. By Seniru

Made by Seniru. Simple Java Password Validator. Source

Entered password: Pass123&&
Valid: true
import java.util.regex.*;
import java.util.*;

public class PasswordValidator {
    
    static boolean isValid(String pw) {
        return pw.length()>4&&pw.length()<11&&Pattern.compile(".*\\d.*").matcher(pw).matches()&&Pattern.compile(".*[a-zA-Z].*").matcher(pw).matches()&&Pattern.compile(".*\\p{Punct}.*").matcher(pw).matches()&&!Pattern.compile(".*\\s.*").matcher(pw).matches();
    }
    
    public static void main(String[] args) {
        var c = new Scanner(System.in).nextLine();
        System.out.println("Entered password: "+c+"\nValid: "+isValid(c
));
    }
}

5. By Deepraj Chawda

Made by Deepraj Chawda. Source

enter 
pass123
false
import java.util.Scanner ;
public class Program
{
    public static void main(String[] args) {
    
     Scanner sc=new Scanner(System.in);
     System.out.println("enter ");
     String p=sc.nextLine();
     int l=p.length();
     int n=0,s=0,sp=0;
     char c[]=p.toCharArray();
     if(l>=5&&l<=10)
     { 
     for(int i=0;i<c.length;i++)
     {
       if(c[i]>=48&&c[i]<=57)
       {
         n++;
       }
       else if(c[i]>=33&&c[i]<=47||c[i]>=58&&c[i]<=64){
         s++;
       }
       else if(c[i]==32)
       {
         sp++;
       }
     }
       if(n>0&&s>0&&sp==0){
        System.out.println("true");
      }
      else{
        System.out.println("false");
      }
       
     
     }else{
       System.out.println("false");
     }
    
     
  }


}

6. By Artem

Made by Artem. Source

Your password is qwerty123

[Ok] Length is more than 5
[Ok] Length is less than 10
[Ok] Your password has numbers
[Error] Your password has no special characters
[Ok] Your password has no spaces
| 
| Your password is not valid.
import java.util.Scanner;

public class Program
{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        int i = 0;
        int Len = str.length();
        char ch = 'a';
        
        int sp = 0; //special items (@, $, %, &, etc)
        int nm = 0; // numbers
        int bl = 0; // spaces
        
     for(i=0;i<Len;i++) { // loop that counts numbers, spaces and special characters
         ch = str.charAt(i);
        if ((ch=='@')||(ch=='#')||(ch=='%')||(ch=='$')||(ch=='&')||(ch=='-')||(ch=='+')||(ch=='*')||(ch=='*')||(ch=='!')||(ch=='?')||(ch=='_')||(ch=='=')||(ch=='.')||(ch==',')){
            ++sp;
}
        if (ch==' '){
            ++bl;
        }
        
        if ((ch=='0')||(ch=='1')||(ch=='2')||(ch=='3')||(ch=='4')||(ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9')){
            ++nm;        
    } 
    } // loop end
    

int c = 0;

// Output
System.out.println("Your password is "+str+"\n");
if (Len >= 5){
   System.out.println("[Ok] Length is more than 5");
   ++c;
}else{
   System.out.println("[Error] Length is less than 5");
}
if (Len <= 10){
   System.out.println("[Ok] Length is less than 10");
   ++c;
}else{
   System.out.println("[Error] Length is more than 10");
}
if (nm >= 1){
   System.out.println("[Ok] Your password has numbers");
   ++c;
}else{
   System.out.println("[Error] Your password has no numbers");
}
if (sp >= 1){
    System.out.println("[Ok] Your password has special characters");
    ++c;
}else{
    System.out.println("[Error] Your password has no special characters");
}
if (bl >= 1){
    System.out.println("[Error] Your password has spaces");
    
}else{
    System.out.println("[Ok] Your password has no spaces");
    ++c;
}
if (c==5){
    System.out.println("| \n| Your password is valid!\n|");
    
    }
    else{
        System.out.println("| \n| Your password is not valid.\n|");
    }

}
}