Is Numeric condition in

Posted on 2012-09-12
Last Modified: 2012-09-24
How to check if the value for a field contains any characters ??It should be all numeric ..can we use Regular expression or Is numeric conditions???

Is the below code correct??Please correct if you see any issues....

 If System.Text.RegularExpressions.Regex.Match(CONDOPROJMNGRPROJID,"\d").Success Then
                Throw New Exception("The  Condo Project Manager ID is a value issued by FNMA and is only numeric")
            End If
        End If
    End Sub

Thanks in advance!!
Question by:onebite2
    LVL 13

    Expert Comment

    I think the advantage of using regular expressions is that you have more flexibility about your validation. IsNumeric will return true with these values "0, 1.5, -1.5" and that might not be your intention.

    If you wish to validate that the number is numerical digits only, no decimal dot (separator), no negatives, etc., regular expressions is the way to go. If you only need to make sure that the field contains a "valid" number (considering the above examples), you can use IsNumeric.
    LVL 35

    Expert Comment

    Public Function IsNumeric(ByVal inputString As String) As Boolean      
    	Dim _isNumber As System.Text.RegularExpressions.Regex = New _
    	Return _isNumber.Match(inputString).Success
    End Function

    Open in new window

    LVL 83

    Expert Comment

    You can use the Integer.TryParse method and it will return true if the string is a valid number.
    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    Regular expressions are useful if you want to check for unusal formats. Regular expressions could accept values that would work on one computer, but not on another because the format of the numerics can change according to the ControlPanel settings.

    If your only aim is to check a file or user input to make sure that it will be accepted in a numeric variable, then TryParse is the way to go.

    However, instead of going straight to Integer.TryParse or Double.TryParse, as most examples do, be sure to match the type on which you call TryParse to the type of any numeric variable you intend to use.

    Integer.TryParse will accept values outside of the 0-255 range allowed in a Byte for instance, so you should use Byte.TryParse if you want to assign to a Byte.
    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    LVL 40

    Accepted Solution


    Anything that is in the Microsoft.VisualBasic namespace is there to help in transiting from classic VB to .NET. Ideally, the methods in that namespace should not be used. For most of them, there are better ones in the framework. Learning to use the methods in the framework will also help if you ever switch to another language, because the framework methods are the same across all .NET languages.

    IsNumeric always evaluate against a Double. It won't take into account the fact that Integer are numeric but do not accept the decimal separator, that Byte is a numeric but does not accept negative numbers.

    That is why the individual TryParse method for each type of numeric value are a better choice over the old function.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    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…
    This video discusses moving either the default database or any database to a new volume.

    779 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

    18 Experts available now in Live!

    Get 1:1 Help Now