Mayank
Mayank

Reputation: 407

regex to allow only numbers and comma in C#

I am using regex ISMatch method to check that string contains only numbers and comma and accept below two types

EX-> 123,456 Accepted

EX-> 123,456, Accepted

I am using below regex but it does not works it pass string with alphabets too

[0-9]+(,[0-9]+)*,?

Can anyone help me ?

Upvotes: 0

Views: 3122

Answers (1)

Hubii
Hubii

Reputation: 348

Here's the simplest regex:

^\d*[,]\d*$

However, this will succeed for just , with no digits. If you require at least one digit either before or after the comma or dot, I think this is it:

^(\d+[,]\d*|\d*[,]\d+)$

If the comma is optional rather than required, add ? after [,].

Upvotes: 1

Related Questions