Check against Numbers and dot in a string

Posted on 2005-05-10
Last Modified: 2010-04-16
Using Regular expressions . I want to check a string. The string must contain only numbers and one dot.

Example Valid Values:

Example Invalid Values:

How can I do this?

Question by:praveenuni
    LVL 7

    Expert Comment

    If you're trying to parse a decimal number, I would recommend using Double.Parse() or Double.TryParse() instead, as they'll handle this task in a much more generic, extensible and globalization-friendly manner than anything you could do with a regular expression.

    If you want to parse this exact syntax for some other reason, this regular expression should do the trick:


    That expression assumes you want to require numbers on the left of the period, while the period itself and the numbers after it are optional. You can change the "+" symbols to "*" to make the banks of numbers optional (for example, changing the second + to a * would  allow the period to appear with no numbers after it). Let me know if this isn't quite what you need.

    Author Comment

    The above regular expression is not checking for numbers + alphabets. for eg: 9abc2
    If I enter the above string its not showing as an invalid string.

    Author Comment

    and its not showing error for eg., 909.909.909

    Any suggestions
    LVL 7

    Accepted Solution

    Ah, want to make sure the entire string consists of nothing but a match to this pattern. In that case, you just need to place start and end anchors around it, like so:


    Author Comment

    Great Ceiled .. That worked ..


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 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

    6 Experts available now in Live!

    Get 1:1 Help Now