Method 1:
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 2:
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 we should enter an integer and it is not negative! ";
Return false;
}
}
Return true;
}
Method 3:
Static bool isnumeric (string Str)
{
System. Text. regularexpressions. RegEx reg1
= New system. Text. regularexpressions. RegEx (@ "^ [-]? \ D + [.]? \ D * $ ");
Return reg1.ismatch (STR );
}
Method 4: (Scalable)
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;
}
}
The regular expression is written as follows:
Static bool isnumeric (string Str)
{
System. Text. regularexpressions. RegEx reg1
= New system. Text. regularexpressions. RegEx (@ "^ [-]? \ D + [.]? \ D * $ ");
Return reg1.ismatch (STR );
}