Avatar of peps03
peps03
 asked on

Only allow numbers, dashes (-) and comma's (,) in text field

Hi,

I would like to only allow numbers, dashes (-) and comma's (,) to be entered into a text field.

How should i do this? Without the use of jQuery please.

Thanks!
JavaScript

Avatar of undefined
Last Comment
peps03

8/22/2022 - Mon
Big Monty

try this:

var someName = document.getElementById("sometextField");
if(/^[-,a-z0-9-]+$/i.test(someName.value))
{
    alert('Name can only be alpha numeric with hypen or comma.');
    return;
}
ASKER CERTIFIED SOLUTION
leakim971

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
peps03

ASKER
Thanks!!!
Your help has saved me hundreds of hours of internet surfing.
fblack61