Search code examples
javaregexhibernatehibernate-validator

Password Validation using hibernate bean validation


I am trying to validate user input password against set of rules and here are those rules.

  1. Minimum 6 and Maximum 8 Character.
  2. Atleast 1 number
  3. Atleast 1 alphabet in capitals.
  4. No Special char allowed

this is what i have done so far in my bean class

@NotNull(message = "{register.pwd.invalid}")
    @Size(min = 6, max = 8, message = "{register.pwd.invalid}")
    public String getPwd()
    {
        return pwd;
    }

I believe for the rest part i have to use regExp but not sure what regular expression i need to have, this is what i came up

^.*(?=.{6,8})(?=.*\d)(?=.*[A-Z]).*$

i am not sure about the expression as i am not good in regExp,can any one help me to point in correcting the regExp


Solution

  • You are quite close

    ^(?=.*\d)(?=.*[A-Z]).{6,8}$
    

    Don't use .* at the start (it would sabotage the length check) and move the length check to the end.