I was attempted 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;
However, I cannot do this. I want individual messages per violated regex constraint on the password field. Is this possible?
My alternative is to use JSF 2.0 f:validatorRegex tags.
You can use alternation (|) operator to combine multiple patterns for your regexp. But in case you have various input and you will have to convert them to instance of Date from a string. Then you must follow in a sequence and validate the input one by one.
\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \. html?\ ' .
2.1 Matching a Single Character The fundamental building blocks of a regex are patterns that match a single character. Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" .
Backslashes in Java. The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.
You can use the inner @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;
Gunnar's solution won't work for me... '.+' in his regexp seem to be missing. However, i'm using Michal's patternList and it works like a charm for me. (Play 2.3.x / Ebean-ORM)
@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 upper letter."), @Pattern(regexp = "(?=.*[!@#$%^&*+=?-_()/\"\\.,<>~`;:]).+", message ="Password must contain one special character."), @Pattern(regexp = "(?=\\S+$).+", message = "Password must contain no whitespace.") }) @Constraints.Required() public String password1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With