The pattern ^\\d{19}$
should be enough.
@Pattern regex doesn't check empty values
-
03-10-2022 - |
Domanda
I have an input field where a SIM number has to be entered. For that field in the bean I put the following validation expression:
@Pattern( regexp = "^(?:\\d{19})$", message = "{validation.notValidSIMNumber}" )
which should be "only 19-digit numbers are accepted".
I expected that if the field is left empty, the validation would also claim.
But instead there is no error message...
EDIT: Sorry, I wrote the false regex (with | at the end). So, to be clear: I need to validate an input field and it must be a 19-digit number. No possibility to leave it empty.
With the above regex, if I leave it empty I get no error message, but as soon as I enter something that's not a 19-digit number I get the error message.
Soluzione
Altri suggerimenti
Your Code is : ^(?:\\d{19}|)$
Change your code to :
@Pattern( regexp = "^(?:\\d{19})$|^$", message = "{validation.notValidSIMNumber}" )
EXPLANATION :
Try:
System.out.println("1234567891234568977".matches("^[0-9]{19}$"));
Hopefully, you are in need of matching 19 digit number and it too be the beginning and end of line it seems.
So for that you can use (^(\d{19})$)* - as it matches only the sentence containing 19 digits or an empty line (without any digits or characters).