Link to home
Start Free TrialLog in
Avatar of onebite2
onebite2

asked on

Is Numeric condition in VB.net

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
   Row.outCONDOPROJMNGRPROJID = GetSafeParsedString(Row.inFNMCONDOPROJMNGRPROJID)
            Else
                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!!
Avatar of Member_2_861731
Member_2_861731
Flag of Canada image

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.
Public Function IsNumeric(ByVal inputString As String) As Boolean      
	Dim _isNumber As System.Text.RegularExpressions.Regex = New _
	System.Text.RegularExpressions.Regex("(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)")       
	Return _isNumber.Match(inputString).Success
End Function

Open in new window

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

http://support.microsoft.com/kb/329488
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.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial