In this article we will introduce with the RegularExpressionValidator control in ASP.NET using C#.
RegularExpressionValidator Example in ASP.NET using C#
RegularExpressionValidator control in ASP.NET is used to check the user input
based on a pattern that you define using RegularExpression. If the value is not
match with the RegularExpression then the error message will be displayed. You
can set the ErrorMessage property which you want to display. The another
property is ControlToValidate in which you can set the attached control
instance. In this example we use the Email address RegularExpression.
The following image shows how can I edit ValidationExpression.
RegularExpressionValidator.aspx (Design Page):
RegularExpressionValidator.aspx (source code):
<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true" CodeFile="RegularExpressionValidator.aspx.cs" Inherits="RegularExpressionValidator" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <div> <h2 style="color: Green"> RegularExpressionValidator in ASP.NET 4 , C#</h2> <strong>Email-id:</strong> <br /> <asp:TextBox ID="emailidTextBox" runat="server" Width="193px" Font-Names="Verdana" /> <asp:Button ID="submitButton" runat="server" Text="Submit" Width="76px" /> <br /> <asp:Label runat="server" Text="Example: suraj@gmail.com" ForeColor="Gray" /> <br /> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="You must enter an email address" ControlToValidate="emailidTextBox" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator> </div> </asp:Content>
Output:
Download source code
[ 0 ] Comments