Regex for password atleast have one Capital , one number minimum 7 and maximum 15 characters

StackOverflow https://stackoverflow.com/questions/14935763

Question

I am using struts 2 annotation validation to validate the password field but it shows error whwn i give the correct one my coding is

@RequiredStringValidator(message = "Please provide a password")
    @StringLengthFieldValidator(minLength="7",maxLength="15",message="The Password should be minmum 7 character and maximum 15 charcters")
    @RegexFieldValidator(expression = "^[0-9A-Z]+$", message = "Password atleast have one Capital and one Number")

any idea?

Était-ce utile?

La solution 2

The regex you are looking for is :

^(.*[0-9].*[A-Z].*)|(.*[A-Z].*[0-9].*)$

The one you provided:

^[0-9A-Z]+$

matches a password composed only of numbers and/or capitals

Autres conseils

I think he really needs (untested): ^.*(?=.{7,15})(?=.*\d)(?=.*[a-zA-Z]).*$

But I wouldn't use a regex... just add a validate method to your action and check it there against your requirements, a simple loop and counter would be a lot easier for most people to understand than your regex which you yourself find difficult to construct and will later find difficult to read.

For instance what if you wanted to change that monstrosity above? If I see a loop over the characters of a string with a counter for numbers and letters that would be very easy to understand and pretty thoughtless to factor that to add uppercase letters, lower case letters and symbols should you choose.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top