top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

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

+1 vote
70 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
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...