In this post under Passay, I will show with example how to verify whether a password has alphanumeric and special characters.
Below is the Main class for our example
Main class
1 package defaultPackage;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Scanner;
6
7 import org.passay.CharacterRule;
8 import org.passay.EnglishCharacterData;
9 import org.passay.PasswordData;
10 import org.passay.PasswordValidator;
11 import org.passay.Rule;
12 import org.passay.RuleResult;
13
14 public class Example4 {
15 public static void main(String[] args) {
16 CharacterRule lowerCaseCharacterRule = new CharacterRule(EnglishCharacterData.LowerCase, 1);
17 CharacterRule upperCaseCharacterRule = new CharacterRule(EnglishCharacterData.UpperCase, 1);
18 CharacterRule digitsCharacterRule = new CharacterRule(EnglishCharacterData.Digit, 1);
19 CharacterRule specialCharacterRule = new CharacterRule(EnglishCharacterData.Special, 1);
20
21 List<Rule> rules = new ArrayList<>(0);
22 rules.add(lowerCaseCharacterRule);
23 rules.add(upperCaseCharacterRule);
24 rules.add(digitsCharacterRule);
25 rules.add(specialCharacterRule);
26
27 PasswordValidator passwordValidator = new PasswordValidator(rules);
28
29 Scanner scanner = new Scanner(System.in);
30 System.out.println("First test");
31 System.out.println("Enter password");
32 String data = scanner.next();
33 PasswordData passwordData = new PasswordData(data);
34 RuleResult ruleResult = passwordValidator.validate(passwordData);
35 System.out.println("Result of password validation: " + ruleResult.isValid());
36 System.out.println("--------------------------------------");
37
38 System.out.println("Second test");
39 System.out.println("Enter password");
40 data = scanner.next();
41 passwordData = new PasswordData(data);
42 ruleResult = passwordValidator.validate(passwordData);
43 System.out.println("Result of password validation: " + ruleResult.isValid());
44 System.out.println("--------------------------------------");
45 }
46 }
In the above code, from line 16 to 19, I create rules that make sure that the input password has atleast 1 lowercase character, 1 uppercase character, 1 digit, and 1 special characters.
I took the help “CharacterRule” class provided by “Passay” framework. It takes two constructor arguments one the type of character and second the minimum number of occurrences of that character type.
Then from line 21 to 25, I created a list which will contain all these rules.
At line 27, I created an instance of “PasswordValidator” class and passed the list of rules as constructor argument.
From line 29 to 35, I asked for 1st input from user, validate it, and print the result of validation to the console.
Similarly at line 38 to 43, I asked for 2nd input from user, validate it, and print the result of validation to the console.
In this way I can verify that a password has alphanumeric and special characters.
Below is the output for your reference
Output
First test
Enter password
Helo1@
Result of password validation: true
--------------------------------------
Second test
Enter password
Helo1
Result of password validation: false