Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Regex @Pattern's for 1 Field?

Tags:

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.

like image 418
Chris Harris Avatar asked Apr 25 '13 21:04

Chris Harris


People also ask

How do I add more than one regex?

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.

What does \\ mean in regex?

\\. 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?\ ' .

How do I match a pattern in regex?

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" .

What does \\ mean in Java regex?

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.


2 Answers

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; 
like image 124
Gunnar Avatar answered Sep 22 '22 00:09

Gunnar


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; 
like image 45
Ganov13 Avatar answered Sep 22 '22 00:09

Ganov13