Ensuring my String only contains numbers.

Posted on 2007-10-11
Last Modified: 2010-04-15
I was just wondering what is the easiest way to check to ensure a string only contains numbers.

I was thinking that perhaps


But perhaps there is a better way

Question by:directxBOB
    LVL 14

    Accepted Solution

    use this ...much more effective

    public bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
        Double result;
        return Double.TryParse(val,NumberStyle,System.Globalization.CultureInfo.CurrentCulture,out result);
    LVL 14

    Expert Comment

    you can also use regular expressions

    using System.Text.RegularExpressions;

     string MyString = "234234234";
                if (Regex.IsMatch(MyString, @"^-?(\d+)(\.\d*)?$"))
                    MessageBox.Show(MyString + " is numeric");
    LVL 7

    Expert Comment

    Regular expressions:

    string str = "\x1811\x1812\x1813";
    Regex reNum = new Regex(@"^\d+$");
    bool isNumeric = reNum.Match(strToTest).Success;

    Regular expression \d digit character will match any Unicode digit.

    Hope this helps

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now