Regular expression validator is to maintain the value of a particular field in a specific pattern. For example, user registration form is having the email field. The supplied email should in the correct format like "name@domain.com". Otherwise, we can treat the supplied email is incorrect. Using regular expression we can check whether the supplied email is in the correct format or not.
Example:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldvalidator.aspx.cs" Inherits="WebProgramming.Validators.RequiredFieldvalidator" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
UserName: <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="UserName is required." ControlToValidate="txtUserName"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>
Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Password is required." ControlToValidate="txtPassword"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>
Confirm password: <asp:TextBox ID="txtConfirmvalidator" runat="server" TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="RangeValidator2" runat="server" ErrorMessage="Confirm password is required." ControlToValidate="txtConfirmvalidator" MinimumValue="18" MaximumValue="35"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvConfirmpassword" runat="server" ControlToValidate="txtConfirmvalidator" ControlToCompare="txtPassword" ErrorMessage="Password mismatch"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
Age: <asp:TextBox ID="txtAge" runat="server"></asp:TextBox><asp:RangeValidator ID="rfvAge" runat="server" ErrorMessage="User age should be with in 18-35." ControlToValidate="txtAge" MinimumValue="18" MaximumValue="35"></asp:RangeValidator></td>
</tr>
<tr>
<td>
Email: <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><asp:RegularExpressionValidator ID="RangeValidator1" runat="server" ErrorMessage="Supplied email is not in correct format." ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Output when wrong format supplied




SQL Server
C#.Net
ASP.Net
ADO.Net
jQuery



No comments:
Post a Comment