• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

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!!
0
onebite2
Asked:
onebite2
1 Solution
 
LIONKINGCommented:
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.
0
 
YZlatCommented:
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

0
 
CodeCruiserCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
 
Mohamed AbowardaSoftware EngineerCommented:
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
@Medo3337

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.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now