RegexpValidator and null

I need i password with the constrains of four digits.
So i create a passwordFields an i add aRegexpValidator with the regular expression “\d{4}”

PasswordField pass = new PasswordField("usuario"); pass.addValidator(new RegexpValidator("\\d{4}", "La contraseña debe tener cuatro caractéres numéricos")); But if i left the field empty the error message doesn’t appear (the field pass the validation).
It fails if i put “1” , “abc”, …
Musn’t it null fail the validation? Why not in this case?

IIRC you need to call setRequired(true). Empty value is treated different than invalid value.

Hello José,
as docimentation states for RegexpValidator: “An empty string or a null is always accepted - use the required flag on fields or a separate validator (or override isValidValue(String)) to fail on empty values.”

To overcome it you can apply to your field also NullValidator() or/and StringLenghtValidator ;
Here is exaple of different validation usage:

Another approach is to set setRequired(true);
Hopefully, it helps : )

Ok thanks so much, Johannes and Anastasia
three alternatives:

  • add NullValidator
  • override the isValidValue
  • setRequired (true)

PD: i remind me to look api, docs and book examples before ask!!! :C