Please refer this code.
HTML
<asp:TextBox ID="txtNumber" runat="server" />
<asp:Button Text="Validate Number" runat="server" OnClick="ValidateNumeric" />
Namespaces
using System.Text.RegularExpressions;
C#
private bool ValidateNumeric(string text)
{
Regex test = new Regex("^[0-9]*$");
return test.IsMatch(text);
}
protected void ValidateNumeric(object sender, EventArgs e)
{
bool b = ValidateNumeric(this.txtNumber.Text.Trim());
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Validation " + b + "')", true);
}
Using Class
class MyValidationClass
{
public bool ValidateNumeric(string text)
{
Regex test = new Regex("^[0-9]*$");
return test.IsMatch(text);
}
}
protected void ValidateNumeric(object sender, EventArgs e)
{
MyValidationClass val = new MyValidationClass();
bool b = val.ValidateNumeric(this.txtNumber.Text.Trim());
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Validation " + b + "')", true);
}
Similarly you can do other validations.