0

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.

3
  • 3
    Are empty values "ok" or not ? If it's not what you want then remove |. It means "or". Since you have nothing after it, it will validate empty string Commented Jan 27, 2014 at 9:51
  • I have no experience in bean-validation, but it sounds like something that cannot be checked from an entry field alone. You should perhaps have a check when the user submits the form or whatever and make sure that every mandatory field is not empty. It wouldn't make sense to prevent an entry field being empty when a user is shown the form, because otherwise, the user will be getting error messages before doing anything at all. Commented Jan 27, 2014 at 11:40
  • @Jerry you're maybe right; there is the annotation @NotNull, but I just thought that the @Pattern is "stronger" and could include the first too... Commented Jan 27, 2014 at 16:38

4 Answers 4

3

The pattern ^\\d{19}$ should be enough.

Sign up to request clarification or add additional context in comments.

Comments

0

Your Code is : ^(?:\\d{19}|)$

enter image description here

Change your code to :

@Pattern( regexp = "^(?:\\d{19})$|^$", message = "{validation.notValidSIMNumber}" )

EXPLANATION :

enter image description here

Comments

0

Try:

System.out.println("1234567891234568977".matches("^[0-9]{19}$"));

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.