3

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Remove the first .* and anchor the char limit, eg:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.