0

I have the following regex to validate only for integers, ^\$?\d{0,10}(.(\d{0,2}))?$ While it correctly identifies 12 as a valid integer, it also validates $12 as a valid one. If, I remove the first $ from the regex it doesn't work either.

Thanks

Massey
  • 1,099
  • 3
  • 24
  • 50
  • That regex appears to be for validating money input (with two decimal places). It's not for integers, and in any case it has at least one bug for money (use of `.` allows input like `$10x00`). You might want to try something more appropriate like [`Convert.ToInt32`](https://msdn.microsoft.com/en-us/library/sf1aw27b(v=vs.110).aspx). – Greg Hewgill Feb 08 '18 at 01:00
  • 2
    This is one of those operations that is just a bit beyond regex. What constitutes a valid integer drastically changes based on the culture. USA and UK use the opposite Decimal and Thousand seperators. Genreally Parse and TryParse are there to validate a integer - by trying to parse it into one. – Christopher Feb 08 '18 at 01:00
  • While I agree with @Christopher that RegEx is not the best tool for validating integers one can build correct locale-specific regex based on CultureInfo (whether it is good idea is open for discussion). – Alexei Levenkov Feb 08 '18 at 01:58
  • Possible duplicate of [Regex Match Integer Only](https://stackoverflow.com/questions/9043551/regex-match-integer-only) – Mick Feb 08 '18 at 02:54
  • or duplicate of .... https://stackoverflow.com/questions/617826/whats-a-c-sharp-regular-expression-thatll-validate-currency-float-or-integer – Mick Feb 08 '18 at 02:55

1 Answers1

0

Short Answer

using System;
using System.Text.RegularExpressions;

Console.WriteLine(Regex.IsMatch("1313123", @"^\d+$")); // true

That assumes we want to validate a simple integer.

Conversation

If we also want to include thousands grouping structures for one or more cultures or other numeric conventions, that will be more complicated. Consider these cases:

  • 10,000
  • 10 000
  • 100,00
  • 100,000
  • -10
  • +10

For a sense of the challenge, see the NumberFormatInfo properties here.

If we need to handle culture specific numeric conventions, it's probably best to leverage the Int64.TryParse() method instead of using Regex.

Sample Code

It's live here. The complete list of NumberStyles is here. We can add/remove the various NumberStyles until we find the set that we want.

using System;
using System.Text.RegularExpressions;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(IsIntegerRegex("")); // false
        Console.WriteLine(IsIntegerRegex("2342342")); // true

        Console.WriteLine(IsInteger("1231231.12")); // false
        Console.WriteLine(IsInteger("1231231")); // true
        Console.WriteLine(IsInteger("1,231,231")); // true
    }

    private static bool IsIntegerRegex(string value)
    {
        const string regex = @"^\d+$";
        return Regex.IsMatch(value, regex);
    }

    private static bool IsInteger(string value)
    {
        var culture = CultureInfo.CurrentCulture;
        const NumberStyles style = 
            // NumberStyles.AllowDecimalPoint | 
            // NumberStyles.AllowTrailingWhite | 
            // NumberStyles.AllowLeadingWhite | 
            // NumberStyles.AllowLeadingSign |
            NumberStyles.AllowThousands;

        return Int64.TryParse(value, style, culture, out _);
    }
}
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467