0

I want to use a regex @Pattern with Hibernate validation. How can I do this regex expression?

[a][b][c][d][e] is a number with 13 digits like this: 1831222132132

a - one digit, 1 or 2, nothing else;
b - from 01 to 99, it is the last 2 digits from the year, for ex 1983 => b = 83;
c - from 01 to 12;
d - from 01 to 31;
e - 6 digits;

Any help will be appreciated. Thank you!

0

1 Answer 1

3

To validate that in Hibernate you can use @Pattern annotation like this

@Column(name = "myInpt")
@Pattern(regexp = "^[12](0[1-9]|[1-9][0-9])(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])([0-9]{6})$", message = "Incorrect format")
private String myInput;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Can you tell me how can I do to show the error message in the response body? Now the message is empty { "timestamp": "2020-06-03T17:20:20.721+00:00", "status": 400, "error": "Bad Request", "message": "", "path": "/ibantoiban" }
In your controller, you should use @valid annotation. Also, you make sure you do not overwrite the default behavior of exception handling in your code. code example:
@PostMapping("/zzz") public ResponseEntity<ZZZ> addzzz(@Valid @RequestBody ZZZ zzz)

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.