J0NNY ZER0
J0NNY ZER0

Reputation: 707

Change this regex to include numbers with hyphen

I have a regex built to exclude all special charecters - /^\s*[a-zA-Z0-9,\s]+\s*$/ How could I change this regex to exclude special characters except for hyphen so users can enter numbers with dashes

Upvotes: 0

Views: 112

Answers (1)

Denis O.
Denis O.

Reputation: 1850

just add dash in your set:

/^\s*[a-zA-Z0-9,\s-]+\s*$/

and you can optimize it a bit:

/^\s*[A-Za-z\d,\s-]+\s*$/

\d - means all digits

@MikeM, you're right. done! Changed back A-z to A-Za-z as @MikeM noticed.

Upvotes: 2

Related Questions