1

I have this expression for valid number only...

^[0-9,-\.]+$

This works fine, but I need to ensure that a maximum of 1 , and 1 . can be entered? This needs to cater for different cultures so it doesn't matter to me which order the , and . come in.

Can anyone help with this?

4
  • 1
    I'd really appreciate examples. Is a floating point number what you want to match? Nov 7, 2012 at 11:19
  • Your regex thinks that .---, is a valid number. I don't. ;) ALso, don't forget that the right type of comma is culture-dependent.
    – Lucero
    Nov 7, 2012 at 11:37
  • I understand why you want to allow no more than one decimal separator, but why only allow one thousands separator? Isn't 1,000,000.00 a valid number? Or 1.000.000,00, depending on your locale? (even 1'000'000,00 and many other variants can be found in real life...) Nov 7, 2012 at 12:06
  • in this scenario, there's no way anyone would enter a number larger than 99,999.99 to be honest, it's a warranty claim form and we know that any number larger than that for travel distance, hours and labour hours would not be correct and would be rejected :) but I understand what you're saying!
    – Stuart
    Nov 7, 2012 at 12:14

3 Answers 3

1

If your regex flavor supports lookaheads you can use:

^(?=[^,],[^,]$)(?=[^.]\.[^.]$)[\d,.-]+$

If not, then you could do something like this:

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

But I find this is rather ugly.

Note that I changed your character class. [0-9] can be shortened to \d (note that dependent on your regex flavor this might match other Unicode digits than just 0-9), . does not need to be escaped inside character classes, and - belong at the beginning or the end of character class, otherwise they need to be escaped. Funnily enough, in your case it does not even make a difference, because ,-. creates a range of characters from , to . (in ASCII order). However all the characters in this range are incidentally ,, - and .. Lucky you ;)

However, both answers really just do what you are explicitly asking for. Your pattern will still match --.--34,---1--2 and stuff like that. If you really want to match a valid number with culture-independent decimal point you should probably use this instead:

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

Of if you want to match a valid number with . decimal point and at most one , thousand-separator (or vice-versa), this:

^-?\d{0,3}(?:(?:,\d{3})?(?:\.\d+)?|(?:\.\d{3})?(?:\,\d+)?)$

I don't think there is a shorter version, really. Do not worry about all the ?:. They just make the subpatterns non-capturing. Since you do not need that anyway, it is usually a significant performance increase.

6
  • Note that \d may match more than just 0-9 (the unicode digit matches more than just those 10 decimal digits; you have arabic digits etc. also in them)
    – Lucero
    Nov 7, 2012 at 11:39
  • @Lucero I guess that depends on the regex flavor, but good point. Nov 7, 2012 at 11:41
  • thanks for all the help, What I need to do is just ensure than a valid number is entered without any text or spaces. So 10 would be acceptable. 10.00 would be acceptable. 10,000.00 would be acceptable as well as the culture equivalents 10.000,00.
    – Stuart
    Nov 7, 2012 at 11:57
  • btw this is in asp.net 4.0 if that helps with the flavour? i dont know if that supports look aheads or not. i'm new to this! :)
    – Stuart
    Nov 7, 2012 at 12:01
  • 1
    @Stuart yes it does. .NET has pretty much the best regex engine out there. I'll edit the last bit of my question to suit your needs. You should usually include these examples in your question, though ;) Nov 7, 2012 at 12:02
0

This is what I would probably use.

/^-?\d{0,2}((\.?\d{3})*(,\d+)?|(,?\d{3})*(\.\d+)?)$/
/*

^                   beginning of string
-?                  0 or 1 minus sign
\d{0,2}             0-2 numbers
(
    (\.?\d{3})*     0 or more (3-digit numbers, alternatively preceded by a dot)
    (,\d+)?         0 or 1 (comma and at least 1 number)
|                   or
    (,?\d{3})*      0 or more (3-digit numbers, alternatively preceded by a comma)
    (\.\d+)?        0 or 1 (dot and at least 1 number)
)
$                   end of string

*/

EDIT: You can change this into

/^-?\d{0,2}((\.?\d{3})*(,\d{1,2})?|(,?\d{3})*(\.\d{1,2})?)$/
                            ^^^                    ^^^

if you want to limit the floating part to 2 digits.

0

You can simply use this regex..it's quite simple and straightforward..

^(\d*|\d*\,\d*|\d*\.\d*|\d*\.\d*\,\d*|\d*\,\d*\.\d*)$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.