Create a class of Regular Expressions for validating TextBox text in ASP.Net

Last Reply on Nov 14, 2014 03:10 AM By Azim

Posted on Nov 13, 2014 03:09 AM

I want to create regular expression class in asp.net

Posted on Nov 14, 2014 03:10 AM

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.

I agree, here is the link: http://e-iceblue.com/free-api