Reputation: 2382
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.
Upvotes: 0
Views: 1946
Reputation: 1
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).
Upvotes: 0
Reputation: 8787
Try:
System.out.println("1234567891234568977".matches("^[0-9]{19}$"));
Upvotes: 0
Reputation: 4864
Your Code is : ^(?:\\d{19}|)$
Change your code to :
@Pattern( regexp = "^(?:\\d{19})$|^$", message = "{validation.notValidSIMNumber}" )
EXPLANATION :
Upvotes: 0