NumberRangeRule example

In this post under Passay, I will explain with example the purpose of “NumberRangeRule” class.

NumberRangeRule takes a range of numbers and verifies whether the user entered password doesn’t contain numbers in that range.

So “NumberRangeRule” constructor takes two arguments
1) starting number (inclusive)
2) ending number (exclusive)

Below is the complete Main class for your reference

Main class

1  package defaultPackage;
2  
3  import java.util.Scanner;
4  
5  import org.passay.NumberRangeRule;
6  import org.passay.PasswordData;
7  import org.passay.PasswordValidator;
8  import org.passay.Rule;
9  import org.passay.RuleResult;
10 
11 public class Example6 {
12     public static void main(String[] args) {
13         Rule numberRangeRule = new NumberRangeRule(8, 16);
14         PasswordValidator passwordValidator = new PasswordValidator(numberRangeRule);
15         Scanner scanner = new Scanner(System.in);
16         System.out.println("First test");
17         System.out.println("Enter password");
18         String data = scanner.next();
19         PasswordData passwordData = new PasswordData(data);
20         RuleResult ruleResult = passwordValidator.validate(passwordData);
21         System.out.println("Result of password validation: " + ruleResult.isValid());
22         System.out.println("--------------------------------------");
23         
24         System.out.println("Second test");
25         System.out.println("Enter password");
26         data = scanner.next();
27         passwordData = new PasswordData(data);
28         ruleResult = passwordValidator.validate(passwordData);
29         System.out.println("Result of password validation: " + ruleResult.isValid());
30         System.out.println("--------------------------------------");
31     }
32 }

Below is the output

Output

First test
Enter password
hello10
Result of password validation: false
--------------------------------------
Second test
Enter password
hello1
Result of password validation: true
--------------------------------------

In case of first test, I entered “hello10” which contains the number 10 which is within the range 8 to 16. So validation will fail.

In case of second test, I entered “hello1” which contains the number 1 which is out of the range 8 to 16. So validation will pass.

Leave a comment