Search code examples
javaregexhibernatehibernate-validator

Hibernate Validator (how to only accept a 4 letters and 10 digits)


I'm trying to validate a String with this format (4 letters and 10 digits):

ABCD1234567890

But this code isn't working

@NotNull(message="tarjeta sanitaria cannot be null")
@Pattern(regexp = "(([A-Z]){4})([0-9]){10}",message = "tarjeta sanitaria is not valid")
private String tarjSanitaria;

Any idea?


Solution

  • I think that you miss start and end of the string in your pattern.

    Maybe this is what you are looking for:

    ^[A-Z]{4}[0-9]{10}$
    

    You can test your regex using this page https://regex101.com/