0

My bean have a field called name. I need to do a validation that Name should not contain (), (.), (,), (-), (/), (), (@).

I tried this but it did not work.I need help.

@Pattern(regexp="^(?!.*( \\s |.|,|-|/|@|' )).*$",message="should not contain ( ), (.), (,), (-), (/), (‘), (@)")

1 Answer 1

3

Use a Character Class (negation) here instead of a Negative Lookahead.

@Pattern(regexp = "^[^ .,‘/@-]*$")

It's not clear if (') should be added since you contain it in your regular expression above but you do not specify that this character should not be contained.

The following might work for you as well depending on how strict this name field should be..

@Pattern(regexp = "^[a-zA-Z0-9]*$")
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Yeah, I agree that some flavor of option 2 would be sensible, otherwise he might exclude all these chars and still end up with ประเทศไทย :)

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.