Passay Tutorial on Passay IllegalCharacterRule

illegalcharacterrule allows to specify the characters which are not allowed in a password. consider the following example.

example

import org.passay.illegalcharacterrule;
import org.passay.numberrangerule;
import org.passay.passworddata;
import org.passay.passwordvalidator;
import org.passay.ruleresult;
import org.passay.whitespacerule;

public class passayexample {
   public static void main(string[] args) {
      //rule: special characters like &, <, > are not allowed in a password 
      illegalcharacterrule illegalcharacterrule 
         = new illegalcharacterrule(new char[] {'&', '<', '>'});

      //rule: 1 to 5 numbers are not allowed
      numberrangerule numberrangerule = new numberrangerule(1, 5);

      //rule: white spaces are not allowed
      whitespacerule whitespacerule = new whitespacerule();

      passwordvalidator validator 
         = new passwordvalidator(illegalcharacterrule,numberrangerule,whitespacerule);
      passworddata password = new passworddata("abc&4d  ef6");
      ruleresult result = validator.validate(password);

      if(result.isvalid()){
         system.out.println("password validated.");
      }else{
         system.out.println("invalid password: " + validator.getmessages(result));            
      }
   }
}

output

invalid password: [
password contains the illegal character '&'.,
password contains the number '4'.,
password contains a whitespace character.]