Menu ▼



Posted By :
avatarmrkraju
Total Views : 533

Check given string contains other than alphabets using regular expression in asp.net and c#

This example shows you how to verify given input contains only alphabets or not using regular expression in asp.net and c#.

Regular expression to check string contains only alphabets and spaces.
string strRegexpAlphabets = @"^[a-zA-Z ]*$";

Regular expression to check string contains only alphabets & not allow spaces (Remove the space after Z and before ]).
string strRegexpAlphabets = @"^[a-zA-Z]*$";

C# Code

protected void btnValidate_Click(object sender, EventArgs e)
{
    string strRegexpAlphabets = @"^[a-zA-Z ]*$";
    if (System.Text.RegularExpressions.Regex.IsMatch(txtAlphabets.Text, strRegexpAlphabets))
    {
        lblMessage.Text = "String contains only alphabets";
        lblMessage.ForeColor = System.Drawing.Color.Green;
    }
    else
    {
        lblMessage.Text = "String contains other than alphabets";
        lblMessage.ForeColor = System.Drawing.Color.Red;
    }
}

ASPX Page

<div>
    Alphabets<br />
    <asp:TextBox ID="txtAlphabets" 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
Regular expression to check string contains only numbers






comments powered by Disqus


Search dotnetlearners.com