سؤال

I need help with a regular expression to allow only numbers, commas, and whitespaces in JavaScript.
For example :

"123456,78889,871253,85879"        valid
",12002, 102878,11000"       &nbsp           valid
"77,1293,abc"                                   not valid (Due to the abc)

So far I have got :

var regex = new RegExp("[^0-9]|[^\,]|[^\s]");
if (regex.exec(myValue) === null) {
     // value is valid
}
هل كانت مفيدة؟

المحلول

Try:

var regex = /^[0-9\,\s]+$/;

Demo

Fiddle

نصائح أخرى

have you tried this one? /^[0-9, ]*$/ it should be enough

http://www.debuggex.com/r/s_4F8WHNl0nyLs0L

/^[0-9,\s]*$/.test("1234565, 123545, abc");

this returns false

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top