Home > Net >  Validation of a string Spring Boot
Validation of a string Spring Boot

Time:08-26

I am using Spring Boot and I want to validate an email. In my DTO I have the field

@NotBlank
@Email
@Length(min = 5, max = 100)
String email

Now I want to validate, that for example the email does not contain subline "@domain.com". I can create my own annotation for validation, that is not a problem. But maybe there are already developed solutions about String format?

Thank You.

CodePudding user response:

As mentioned in answer above, you can use regex,@Email annotation accepts regexp as argument.

In your specific case, to validate that email doesn't contain string @domain.com you can use following regex:

 @Email(regexp ="^(([email protected]).)*$")
    @NotBlank
    @Length(min = 5, max = 100)
    String email;

CodePudding user response:

Java Validation APIs provide a @Pattern annotation which accepts a regex:

https://docs.oracle.com/javaee/7/api/javax/validation/constraints/Pattern.html

  • Related