Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Why does the following pattern validation not work
@Pattern(regexp = "^[a-zA-Z0-9_-]*$", message = "Only alpha-numeric characters are allowed")
  private String recipientId;

I want to allow only alpha-numeric characters but above validation does not work as i expect. It allows a request which has a plus sign (+) to pass through

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

When using brackets, hyphen is special character for defining range so you should escape it, if you want to match "-".

The regexp should looks like

[a-zA-Z0-9_\\-]*

Note : After testing it seems that you can use hyphen without escaping it when using brackets.

I would prefer this way (\\w|-)*

\w A word character: [a-zA-Z_0-9]


The issue is maybe not on the regexp but maybe on the way the validation annotation is used. You can use the annotation on the field directly or on the getter but not on both. Are you sure that you always follow the same coding pattern for all annotations?

Finally do you use the annotation @Valid to actually validate your input?

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!