Sandeep Singh
Sandeep Singh

Reputation: 7990

I need a single regex to check number comma and number

I need a regX which can match like 123,123 only. My regX is

var regX = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])*$/;

but its currently matching 123,123 and 123, as well

Valid case: 123,123 or 123,000 or 000,000 Invalid case: 123.123 or 123?123 or '123'.'123'

Upvotes: 0

Views: 77

Answers (4)

user1928066
user1928066

Reputation:

you should use this regex = \d+(,\d+)+

Upvotes: 2

Simon
Simon

Reputation: 1

If your numbers are positive integers you can use: \d+,\d+ If you want floating point numbers as well: (\d|.)+,(\d|.)+ although this will also match malformed numbers with multiple or misplaced decimal points including .,. etc.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

It looks like you're actually trying to match a number with thousand separators.

Try this: /\d{1,3}(?:,\d{3})*/

Upvotes: 0

Brigand
Brigand

Reputation: 86220

You might want to use the {x,y} quantifier. I matches at least X of the item, and at most Y. If you leave one out, it has no limit in that direction. If you just have one number, with no comma it matches exactly that amount.

Exactly three digits:

(\d{3}),(\d{3})

Three or more

(\d{3,}),(\d{3,})

Between 2 and 7 digits:

(\d{2,7}),(\d{2,7})

And so on...

Upvotes: 1

Related Questions