5

I have a bean with this validation:

@Email(message = "Email is not valid", regexp="{(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])}")
@NotEmpty(message = "Email cannot be empty")
private String email;

when the email is empty the message is fine, but when I enter this text

perisava.util.Random@1d7f0036

no error is shown

en Peris
  • 1,537
  • 12
  • 31
  • 63

3 Answers3

9

javax.validation.constraints.Email annotation depends on Jakarta Bean Validation providers for validation. Hibernate Validator is the only compliant validator for this. By default hibernate vaildator matches the email to regex .* unless you provide the regex yourself. Hibernate email validator source code is here.

One solution is to provide your own email regex

@Email(message = "Email is not valid", regexp = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$")
@NotEmpty(message = "Email cannot be empty")
private String email;

The official standard is RFC 5322. But it does not cover all cases for modern emails. You can find more solutions here. Even then regex would be up to you to decide.

Shawrup
  • 2,478
  • 2
  • 12
  • 21
  • I've tried with this error: java.util.regex.PatternSyntaxException: Illegal repetition {(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])} at java.base/java.util.regex.Pattern.error(Pattern.java:2010) – en Peris Dec 19 '20 at 16:21
  • 1
    I have tried the solution and it worked for me :( . I am adding my solution. I am using java 8 with spring boot 2.3.4 – Shawrup Dec 19 '20 at 16:34
  • I tried `perisava.util.Random@1d7f0036.com`, but it failed. This could be a valid email address. – jumping_monkey Feb 15 '23 at 07:06
2

This problem is solved by viewing the source code. @Email annotation has an attribute regexp(), the default is'.*'. And it matches the characters you enter, you can write your own regular expression enter image description here

enter image description here

HaiZi
  • 179
  • 1
  • 7
0

The regex in the accepted answer is hard to read and did not work for the use-case i was testing. Here's another regex that you can use:

private static final Pattern EMAIL = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);

public static boolean isEmail(String s) {
    return EMAIL.matcher(s).matches();
}

It is simpler and it works well.

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58