|
Asp .net How to check if a string is numeric
This is the code I wrote to check if the string is numeric else it puts another value
Dim strCapAmount = ddlCapAmount.Text
Dim itemp As Integer
If Integer.TryParse(strCapAmount, itemp) Then
Else
strCapAmount = 10000000
End If
Here's the full example i found on the net
--------------------------------------------------------------
string numeric = "12345";
string nonNumeric = "abcde";
int value;
if (int.TryParse(numeric, out value))
{
Console.WriteLine("Value: {0}", value);
}
else
{
Console.WriteLine("Conversion failed.");
}
if (int.TryParse(nonNumeric, out value))
{
Console.WriteLine("Value: {0}", value);
}
else
{
Console.WriteLine("Conversion failed.");
}
/* OUTPUT
Value: 12345
Conversion Failed.
*/
Alternate code
public bool isNumeric
(string val, System.Globalization.NumberStyles NumberStyle, string CultureName)
{
Double result;
return Double.TryParse(val,NumberStyle,new System.Globalization.CultureInfo
(CultureName),out result);
}
call the code to check if integer
isNumeric("42000", System.Globalization.NumberStyles.Integer)
|
|