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.RegularExpress ions.Regex .Match(CON DOPROJMNGR PROJID,"\d ").Success Then
Row.outCONDOPROJMNGRPROJID = GetSafeParsedString(Row.in FNMCONDOPR OJMNGRPROJ ID)
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!!
Is the below code correct??Please correct if you see any issues....
If System.Text.RegularExpress
Row.outCONDOPROJMNGRPROJID
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!!
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
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
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.