2

I need a regular expression validation expression that will

ALLOW

  • positive number(0-9)
  • , and .

DISALLOW

  • letter(a-z)
  • any other letter or symbol except . and ,

for example, on my asp.net text box, if I type anything@!#--, the regular expression validation will disallow it, if I type 10.000,50 or 10,000.50 it should allowed.

I've been trying to use this regex:

^\d+(\.\d\d)?$

but my textbox also must allow , symbol and I tried using only integer regex validation, it did disallow if I type string, but it also disallow . and , symbol while it should allow number(0-9) and also . and , symbol

Jerry
  • 67,172
  • 12
  • 92
  • 128
random_dude
  • 95
  • 3
  • 9
  • `.` is special symbol in regex, you should backslash it `\.` – Uriil Jun 27 '14 at 08:14
  • 1
    this might help you. http://stackoverflow.com/questions/16148034/regex-for-number-with-decimals-and-thousand-seperator – Jakub Jun 27 '14 at 08:14
  • 1
    [Related](http://stackoverflow.com/questions/4246077/matching-numbers-with-regular-expressions-only-digits-and-commas/4247184#4247184) – Jerry Jun 27 '14 at 08:15

5 Answers5

4

Don't Use \d to match [0-9] in .NET

First off, in .NET, \d will match any digits in any script, such as:

654۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦᠖

So you really want to be using [0-9]

Incomplete Spec

You say you want to only allow "digits, commas and periods", but I don't think that's the whole spec. That would be ^[0-9,.]+$, and that would match

...,,,

See demo.

Tweaking the Spec

It's hard to guess what you really want to allow: would 10,1,1,1 be acceptable?

We could start with something like this, to get some fairly well-formed strings:

^(?:[0-9]+(?:[.,][0-9]+)?|[1-9][0-9]{0,2}(?:(?:\.[0-9]{3})*|(?:,[0-9]{3})*)(?:\.[0-9]+)?)$

Play with the demo, see what should and shouldn't match... When you are sure about the final spec, we can tweak the regex.

Sample Matches:

0
12
12.123
12,12
12,123,123
12,123,123.12456
12.125.457.22

Sample Non-Matches:

12,
123.
1,1,1,1
zx81
  • 38,175
  • 8
  • 76
  • 97
  • Ha, and I thought I'd be the first one to point out the `^[0-9,.]+$` thing lol – Jerry Jun 27 '14 at 09:35
  • @Jerry No my friend, I was quite surprised when I got to the question and saw the 4 answers! :) – zx81 Jun 27 '14 at 09:36
2

Your regex would be,

(?:\d|[,\.])+

OR

^(?:\d|[,\.])+$

It matches one or more numbers or , or . one or more times.

DEMO

Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
1

Maybe you can use this one (starts with digit, ends with digit):

(\d+[\,\.])*\d+

If you need more sophisticated price Regex you should use:

(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)?

Edit: To make it more reliable (and dont get 00.50) you can add starting and ending symbol check:

(^|\s)(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)($|\s)?
  • 2
    matches `54۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦` – zx81 Jun 27 '14 at 09:33
0

Your format is a bit strange as it is not a standard format. My first thought was to put a float instead of a string and put a Range validation attribute to avoid negative number. But because of formatting, not sure it would work. Another way is the regex, of course. The one you propose means : "some numbers then possibly a group formed by a dot and two numbers exactly". This is not what you exepected.

Strictly fitted your example of a number lower than 100,000.99 one regex could be : ^[0-9]{1-2}[\.,][0-9]{3}([\.,][0-9]{1-2})?$ A more global regex, that accept all positive numbers is the one posted by Avinash Raj : (?:\d|[,\.])+

artragis
  • 3,797
  • 1
  • 15
  • 28
  • which one? I replace the `\d` shortcut by [0-9] in my first regex. Should be ok. – artragis Jun 27 '14 at 10:55
  • Nice that you fixed the `54۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦` problem. :) But there are serious problems remaining in your `^[0-9]{1-2}[\.,]` etc, as you will discover if you test it. :) – zx81 Jun 27 '14 at 11:01
0

I think the best regex for your condition will be :

^[\d]+(?:,\d+)*(?:\.\d+)?$

this will validate whatever you like

and at the same time:

not validate:

  • numbers ending in ,
  • numbers ending in .
  • numbers having . before comma
  • numbers having more than one decimal points

check out the demo here : http://regex101.com/r/zI0mJ4

aelor
  • 9,803
  • 2
  • 27
  • 43