raghurox27
raghurox27

Reputation: 3

How to allow all special characters in textbox only one time using JS?

My textbox should accept only special characters only once. for example: It should not allow '%' symbol more than once. All the special characters should be allowed only once in my textbox in JS. I tried so many answers in stackoverflow but failed to get the exact code. 'Like so'

`/^(?=[a-zA-Z,]*['.-][a-zA-Z,]*$)[a-zA-Z,'.-]+$/`

Here instead of apostrophe, dash and period , I tried '%' but its allowing as many Percentages (%) as can.

Upvotes: 0

Views: 1141

Answers (1)

Nick Retallack
Nick Retallack

Reputation: 19571

Write an event handler that catches keypresses on your textarea. Have it check event.which against the characters in the textarea. If it isn't allowed, return false or call event.preventDefault() and the character will not be inputted.

Upvotes: 1

Related Questions