In this post, we will see how to validate a password in java.
Here are rules for password:
- Must have at least one numeric character
- Must have at least one lowercase character
- Must have at least one uppercase character
- Must have at least one special symbol among
@#$%
- Password length should be between 8 and 20
Table of Contents
Using regex
Here is standard regex for validating password.
1 2 3 |
^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20}$ |
Here is explanation for above regex
^
asserts position at start of a lineGroup
(?=.*\\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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexCurrencySymbol { public static void main(String args[]) { String password1 = "Java2blog@"; String regex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20}$"; boolean validPassword = isValidPassword(password1,regex); System.out.println("Java2blog@ is valid password:" +validPassword); String password2 = "helloword#123"; String regex1 = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20}$"; boolean validPassword1 = isValidPassword(password2,regex1); // No upper case System.out.println("helloword#123 is valid password:" +validPassword1); } public static boolean isValidPassword(String password,String regex) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(password); return matcher.matches(); } } |
Output:
Java2blog@ is valid password:true
helloword#123 is valid password:false
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package org.arpit.java2blog; public class PasswordValidatorMain { public static void main(String args[]) { String password1 = "Java2blog@"; boolean validPassword = isValidPassword(password1); System.out.println("Java2blog@ is valid password:" +validPassword); String password2 = "helloword#123"; boolean validPassword1 = isValidPassword(password2); // No upper case System.out.println("helloword#123 is valid password:" +validPassword1); } /** * Method to check if password is valid or not. * @param password * @return boolean */ public static boolean isValidPassword(String password) { boolean isValid = true; if (password.length() > 15 || password.length() < 8) { System.out.println("Password must be less than 20 and more than 8 characters in length."); isValid = false; } String upperCaseChars = "(.*[A-Z].*)"; if (!password.matches(upperCaseChars )) { System.out.println("Password must have atleast one uppercase character"); isValid = false; } String lowerCaseChars = "(.*[a-z].*)"; if (!password.matches(lowerCaseChars )) { System.out.println("Password must have atleast one lowercase character"); isValid = false; } String numbers = "(.*[0-9].*)"; if (!password.matches(numbers )) { System.out.println("Password must have atleast one number"); isValid = false; } String specialChars = "(.*[@,#,$,%].*$)"; if (!password.matches(specialChars )) { System.out.println("Password must have atleast one special character among @#$%"); isValid = false; } return isValid; } } |
Output:
Java2blog@ is valid password:true
Password must have atleast one uppercase character
helloword#123 is valid password:false
Password must have atleast one uppercase character
helloword#123 is valid password:false
That’s all about how to validate password in java.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.