How to validate password using Java Regular Expression

In this tutorial we are going to see about how to validate password using Java Regular Expression

Password Regular Expression Pattern

((?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[@#^$%!&]).{8,14})

PasswordValidator.java

package com.ehowtonow.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordValidator {
 private static final String PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#^$%!&]).{8,14})";

 public static void main(String[] args) {

  System.out.println("Validate Valid pattern - Abc12@abc : "+validate("Abc12@abc"));
  System.out.println("Validate No Uppercase - JTC123 : "+validate("abc12@abc!"));
  System.out.println("Validate No Upper case No Speacial character - abc1234 : "+validate("abc1234"));
  System.out.println("Validate Less than 8 character - aBc@1! : "+validate("aBc@1!"));
  System.out.println("Validate More than 14 characer @eHowToNowDotCom : "+validate("@eHowToNowDotCom"));

 }

 private static boolean validate(String password) {
  Pattern pattern = Pattern.compile(PATTERN);
  Matcher matcher = pattern.matcher(password);
  return matcher.matches();

 }

}

Output :

Validate Valid pattern – Abc12@abc : true
Validate No Uppercase – JTC123 : false
Validate No Upper case No Speacial character – abc1234 : false
Validate Less than 8 character – aBc@1! : false
Validate More than 14 characer @eHowToNowDotCom : false


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. How to validate password using Java Regular Expression
  2. How to validate IPv4 Address using Java Regular Expression
  3. Java RegEx to validate Date – dd/mm/yyyy pattern
  4. How to replace all occurrences of a string using RegEx
  5. How to validate MAC Address using Java Regular Expression
  6. How to validate Email Address using Java Regular Expression
  7. How to validate IPv4 Address with Port using Java Regular Expression
  8. How to validate Username using Java Regular Expression

Be the first to comment

Leave a Reply