Validate password in java

In this post, we will see how to validate a password in java.
Here are rules for password:

  1. Must have at least one numeric character
  2. Must have at least one lowercase character
  3. Must have at least one uppercase character
  4. Must have at least one special symbol among @#$%
  5. Password length should be between 8 and 20

Using regex

Here is standard regex for validating password.

Here is explanation for above regex

^ asserts position at start of a line
Group (?=.*\\d)
Assert that the Regex below matches
.* matches any character (except for line terminators)
\\ matches the character \ literally (case sensitive)
d matches the character d literally (case sensitive)
Group (?=.*[a-z])
Assert that the Regex below matches
.* matches any character (except for line terminators)
Match a single character present in the list below [a-z]
a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
Group (?=.*[A-Z])
Assert that the Regex below matches
.* matches any character (except for line terminators)
Match a single character present in the list below [A-Z]
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
Group (?=.*[@#$%])
Assert that the Regex below matches
.* matches any character (except for line terminators)
Match a single character present in the list below [@#$%]
@#$% matches a single character in the list @#$% (case sensitive)
.{8,20} matches any character (except for line terminators)
{8,20} Quantifier — Matches between 8 and 20 times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of a line

Here is java program to implement it.

Output:

Java2blog@ is valid password:true
helloword#123 is valid password:false

Using String’s matches method

We can also use string’s matches() method to validate password in java.
Let’s see with the help of an example.

Output:

Java2blog@ is valid password:true
Password must have atleast one uppercase character
helloword#123 is valid password:false

That’s all about how to validate password in java.

Related Posts

  • 29 September

    Input validation in java using Scanner

    In this post, we will see how to do input validation in java using Scanner class. The Java Scanner class is used to get input from user. It provides several methods to get input of different types and to validate the input as well. Here we will see some of these methods that can be […]

  • 04 May

    Java regex for currency symbols

    In this post, we will see about regex to find currency symbols in a text. You can use below regex to find currency symbols in any text. \\p{Sc} Each unicharacter belongs to certain category and you can search for it using /p. Sc is short code for current symbol, so using \p{Sc}, we are trying […]

  • 04 May

    Java credit card validation – Luhn Algorithm in java

    In this post, we will see about Luhn Algorithm in java Introduction Luhn algorithm, also known as modulus 10 or mod 10 algorithm, is a simple checksum process for validating various identification numbers such as credit card numbers, Canadian social securities numbers. This algorithm is designed to protect again mistyped or accidental error rather than […]

  • 21 March

    Java isNull method

    In this post, we will about Objects class’s isNull method. Objects’s isNull() method is used to check if object is null or not. java.util.Objects class was introduced in java 7. Here is simple example for Object's isNull method. [crayon-60090d2241056600224169/] Output: Is str1 null: false Is str2 null: true Let’s look at source code Object’s isNull() […]

  • 25 July

    Validate phone number in java

    In this post, we will see how to validate phone number in java. Validate any international phone number as per E.123 E.123 is a standards-based recommendation by the International Telecommunications Union sector ITU-T. E.123 provides following specifications. The leading plus (+) serves as an international prefix symbol, and is immediately followed by the country code […]

  • 11 July

    Validate email address in java

    In this post, we will see how to validate email address in java. There are times when you have to validate email address provided by user. You might want to write your own logic to validate email addresses but there are lots of standard regular expressions or libraries which can help you validate email address […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.