Posted by : Anwar Hossain
Category : How to use different types of validation control using asp.net c#

How to use RegularExpressionValidator Control in ASP.NET using C#

Dear viewers hear I will show how to use RegularExpressionValidator control . Sometimes we need to take to prevent user to input invalid data in textbox for data restriction purpose in this case we can easily use RegularExpressionValidator control. Client side validation can be implemented by using this control. RegularExpressionValidator can be used for Email Validation, Minimum characters, Maximum characters, Lowercase, Uppercase and Combination of Uppercase Lowercase number.

RegularExpressionValidator ASP.NET C#

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title></title>
  <style type="text/css">
  .style1
  {
  width: 100%;
  }
  .style2
  {
  color: #FF9900;
  }
  .style3
  {
  color: Teal;
  }
  .requiredFieldValidateStyle
  {
   color: Red;
  }
  </style>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <span class="style3"><b style="font-weight: bold; font-size: 14">Email Validation example</b></span>
  <table class="style1">
  <tr>
  <td width="150">
  Enter your your email
  </td>
  <td width="5px">
  :
  </td>
  <td>
  <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Please Enter you valid email"
  ControlToValidate="txtEmail" CssClass="requiredFieldValidateStyle" ForeColor="Red"
  ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
  </asp:RegularExpressionValidator>
  </td>
  </tr>
  </table>
  <span class="style3"><b style="font-weight: bold; font-size: 14">Maximum character length
  Validation(allowed only 10 character) </b></span>
  <table class="style1">
  <tr>
  <td width="150">
  UserName
  </td>
  <td width="5px">
  :
  </td>
  <td>
  <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
  <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="txtUserName"
  ID="RegularExpressionValidator1" ValidationExpression="^[\s\S]{0,8}$" runat="server"
  ErrorMessage="Maximum 8 characters allowed." CssClass="requiredFieldValidateStyle"></asp:RegularExpressionValidator>
  </td>
  </tr>
  </table>
  <span class="style3"><b style="font-weight: bold; font-size: 14">Combination of UpperCase
  LowerCase, Number and Special Character </b></span>
  <table class="style1">
  <tr>
  <td width="150" valign="top">
  Enter Your Password
  </td>
  <td width="5px" valign="top">
  :
  </td>
  <td>
  <asp:TextBox ID="txtCombinatio" runat="server"></asp:TextBox>
  <asp:RegularExpressionValidator ID="RegCombination" runat="server" ControlToValidate="txtCombinatio"
  ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{6,}"
  ErrorMessage=" Minimum 6 characters at least 1 UpperCase ,<br> 1 LowerCase  1 Number and 1 Special Character"
  ForeColor="Red" />
   </td>
  </tr>
  </table>
  <span class="style3"><b style="font-weight: bold; font-size: 14">Combination of UpperCase
  ,LowerCase,<br />
  with Maximum and Minimum character &nbsp;<br />
  Number with Special Character </b></span>
  <table class="style1">
  <tr>
  <td width="150">
  Enter Your Password
  </td>
  <td width="5px">
  :
   </td>
  <td>
  <asp:TextBox ID="txt2npassword" runat="server"></asp:TextBox>
  <asp:RegularExpressionValidator ID="Reg2nPassword" runat="server" ControlToValidate="txt2npassword"
  ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{6,12}"
  ErrorMessage=" Minimum 6 and Maximum 12 characters atleast<br> 1 UpperCase 1 LowerCase, 1 Number and 1 Special Character"
  ForeColor="Red" />
  </td>
  </tr>
  </table>
  </div>
  </form>
</body>
</html>

Out Put

RegularExpressionValidator-in-ASP.Net-C#



Realted Article Headline

How to use RequiredFieldValidator for RadioButtonList in ASP.NET C#
How to use RequiredFieldValidator for DropDownList in ASP.NET C#
How to use RangeValidator in ASP.NET C#
how to use RequiredFieldValidator in ASP.NET C#
How to use RegularExpressionValidator Control in ASP.NET using C#
How to use Custom Validator control using asp.net c#
How to use compare validation control using asp.net c#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection