1 field of multiple regex @ patterns?
•
Java
I tried to apply multiple @ pattern annotations to a single field:
@Pattern(regexp = "(?=.*[0-9])",message = "Password must contain one digit.") @Pattern(regexp = "(?=.*[a-z])",message = "Password must contain one lowercase letter.") @Pattern(regexp = "(?=.*[A-Z])",message = "Password must contain one uppercase letter.") @Pattern(regexp = "(?=\S+$)",message = "Password must contain no whitespace.") private String password;
But I can't do that I want individual messages that violate regular expression constraints on password fields Is that possible?
My alternative is to use the JSF 2.0 F: validatorregex tag
Solution
You can use the internal @ list annotation of @ pattern:
@Pattern.List({ @Pattern(regexp = "(?=.*[0-9])",message = "Password must contain one digit."),@Pattern(regexp = "(?=.*[a-z])",message = "Password must contain one lowercase letter."),@Pattern(regexp = "(?=.*[A-Z])",message = "Password must contain one uppercase letter."),@Pattern(regexp = "(?=\\S+$)",message = "Password must contain no whitespace.") }) private String password;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码