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?

有帮助吗?

解决方案 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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top