20/02/2010
Check If The Input Is Numaric Or Not
To check if the input from text box is numaric or not, can we use the following idea:
1- In .aspx page, create TextBox control, and rename it to txtInput.
2- Create Label control, and rename it to lblResult.
3- Create Button control, and rename it to btnCheck.
4- In code behind, write the following code:
protected void Page_Load(object sender, EventArgs e)
{
if (IsNumeric(txtInput.Text, IsNumericType.Integer))
{
lblResult.Text = "The Input Is Numeric";
}
else
{
lblResult.Text = "The Input Is Not Numeric";
}
}
public enum IsNumericType : int
{
Integer = 0,
Decimal = 1,
Double = 2,
Int64 = 3,
Byte = 4,
Float = 5
}
public static bool IsNumeric(string Input, IsNumericType NumericType)
{
try
{
switch (NumericType)
{
case IsNumericType.Byte:
byte ByteResult = byte.Parse(Input);
return true;
case IsNumericType.Decimal:
decimal DecResult = decimal.Parse(Input);
return true;
case IsNumericType.Double:
double DblResult = double.Parse(Input);
return true;
case IsNumericType.Float:
float FltResult = float.Parse(Input);
return true;
case IsNumericType.Int64:
Int64 I64Result = Int64.Parse(Input);
return true;
case IsNumericType.Integer:
int intResult = int.Parse(Input);
return true;
default:
return false;
}
}
catch { return false; }
}
5- Test it.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment