angular reactive form pattern validator only allow text, numbers and some characters

Viewed 3197

I am using angular 10 and reactive forms. I need a input to only accept (letters, numbers, and this 2 characters "_" and "-") I've tried letters and numbers with this:

 Validators.pattern('[a-zA-Z ][0-9]*')]

The above only works if I do it this way:

Validators.pattern('[a-zA-Z ]*')]

But I also need it to allow numbers and - and _

How can I do this?

1 Answers

Your code become

Validators.pattern('[-_a-zA-Z0-9]*')]

Edit: Based on comments by @ekalin

Related