C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET RegularExpressionValidator ControlThis validator is used to validate the value of an input control against the pattern defined by a regular expression. It allows us to check and validate predictable sequences of characters like: e-mail address, telephone number etc. The ValidationExpression property is used to specify the regular expression, this expression is used to validate input control. RegularExpression Properties
ExampleHere, in the following example, we are explaining how to use RegularExpressionValidator control to validate the user input against the given pattern. // RegularExpressionDemo.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RegularExpressionDemo.aspx.cs" Inherits="asp.netexample.RegularExpressionDemo" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table class="auto-style1"> <tr> <td class="auto-style2">Email ID</td> <td> <asp:TextBox ID="username" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"ControlToValidate="username" ErrorMessage="Please enter valid email" ForeColor="Red"ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator> </td> </tr> <tr> <td class="auto-style2"></td> <td> <br/> <asp:Button ID="Button1" runat="server" Text="Save"/> </td> </tr> </table> </div> </form> </body> </html> Output: It produces the following output when view in the browser. It will validate email format as we specified in regular expression. If validation fails, it throws an error message.
Next TopicASP.NET RequiredFieldValidator
|