top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+1 vote
387 views

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 ]*$";

posted Feb 1, 2016 by Latha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

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

enter image description here

answer Feb 1, 2016 by Shivaranjini
...