Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  55] [ 5]  / answers: 1 / hits: 24056  / 11 Years ago, thu, july 4, 2013, 12:00:00

I want to write a regular expression using javascript or jquery to allow
comma delimited list of numbers OR
space delimited numbers OR
comma followed by a space delimited numbers OR
a combination of any of the above ex
anything that is not a digit, space or comma must be rejected



SHOULD PASS
111,222,333
111 222 333
111, 222, 333
111,222,333 444 555 666, 111, 222, 333,



should NOT pass:
111,222,3a
3a
111 222 3a etc etc



I tried the code below they seemed to work however when I typed
3a as a number, it PASSED!!! How? I cannot understand how my code allowed that letter to pass.



I want to reject anything that is not a space, comma or digit



or is there a better way to do this without regular expressions?
I looked in google and did not find any answer.



Thank you in advance for any help.



var isNumeric = /[d]+([s]?[,]?[d])*/.test(userInput);
var isNumeric = /^[ds,]*/.test(userInput);
var isNumeric = /^[d]*[s,]*/.test(userInput);
var isNumeric = /^[ds,]*/.test(userInput);
var isNumeric = /d+s*,*/.test(userInput);

if (isNumeric == false) {
alert(isNumeric);
return false;
}
else
alert('is Numeric!!!');

More From » jquery

 Answers
49

Would the regular expression
^[d,s]+$
not do the trick?


[#77196] Wednesday, July 3, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacyl

Total Points: 131
Total Questions: 105
Total Answers: 94

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;