RHPT RHPT - 1 month ago 6
Javascript Question

RegEx Pattern to Match Only Certain Characters

I'm a newbie to RegEx, and this particular issue is flummoxing me. This is for use with a JavaScript function.

I need to validate an input so that it matches only this criteria:


  • Letters A-Z (upper and lowercase)

  • Numbers 0-9

  • The following additional characters: space, period (.), comma (,), plus (+), and dash (-)



I can write a pattern easily enough for the first two criteria, but the third one is more difficult. This is the last pattern I managed, but it doesn't seem to work with the test string
Farh%%$$+++,


Any help would be greatly appreciated.

Thank you.

Answer

I just tested this out, and it seems to work at least from my first round testing.

^[a-zA-Z 0-9\.\,\+\-]*$