Menu ▼



Posted By :
avatarmrkraju
Total Views : 405

Validate given string contains other than numbers in asp.net and c#

This example shows you how to check given input contains only numbers (digits) or not using regular expression in asp.net and c#.

Regular expression
string strRegexpNumber = @"^[0-9 ]*$";

C# Code

protected void btnValidate_Click(object sender, EventArgs e)
{
    string strRegexpNumber = @"^[0-9 ]*$";
    if (Regex.IsMatch(txtNumber.Text, strRegexpNumber))
    {
        lblMessage.Text = "Valid Number";
        lblMessage.ForeColor = System.Drawing.Color.Green;
    }
    else
    {
        lblMessage.Text = "Invalid Number";
        lblMessage.ForeColor = System.Drawing.Color.Red;
    }
}

ASPX Page

<div>
    Number<br />
    <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Button ID="btnValidate" runat="server" Text="Validate" OnClick="btnValidate_Click" />
    <br />
    <br />
    <asp:Label ID="lblMessage" runat="server"></asp:Label>
</div>

Output
number regular expression






comments powered by Disqus


Search dotnetlearners.com