Method One:
static bool IsNumeric (String str)
{
if (Str==null | |) str. length==0)
return false;
foreach (char c in str)
{
if (! Char.isnumber (c))
{
return false;
}
}
return true;
}
Method Two:
private bool IsNumeric (string s)
private bool IsNumeric (string s)
{
Char ch0 = ' 0 ';
Char ch9 = ' 9 ';
for (int i=0 i < s.length; i++)
{
if ((S[i] < Ch0 | | s[i] > CH9))
{
This.lblwarning.text= "Here you should enter an integer and not a negative!" ";
return false;
}
}
return true;
}
Method Three:
static bool IsNumeric (String str)
{
System.Text.RegularExpressions.Regex REG1
= new System.Text.RegularExpressions.Regex (@ "^[-]?\d+[.")? \d*$ ");
Return REG1. IsMatch (str);
}
Method four: (extensible)
public static bool Isconvert (String Expression,type DataType)
{
Switch (datatype.name)
{
Case "Double":
Try
{
Double.Parse (Expression);
return true;
}
Catch
{
return false;
}
Case "DateTime":
Try
{
DateTime.Parse (Expression);
return true;
}
Catch
{
return false;
}
Default
return true;
}
}
C # method to verify whether the input is numeric
In fact, you can use regular expressions
static bool IsNumeric (String str)
{
if (Str==null | |) str. length==0)
return false;
foreach (char c in str)
{
if (! Char.isnumber (c))
{
return false;
}
}
return true;
}
Regular expressions are written in the following way:
static bool IsNumeric (String str)
{
System.Text.RegularExpressions.Regex REG1
= new System.Text.RegularExpressions.Regex (@ "^[-]?\d+[.")? \d*$ ");
Return REG1. IsMatch (str);
}