Validators Explained in ASP.NET

Introduction

 
In this blog, I will explain the Validators in ASP.Net.The validation control is used to implement a page level validation in server controls.
 
Types
  • RequiredFieldValidator
  • RangeValidator
  • RegularExpressionValidator
  • CompareValidator
  • ValidationSummary
  • CustomValidator

RequiredFieldValidator

 
Indicates the input Control is not empty when the form is submitted.
 
Example
  1. <asp:TextBox ID="txtFname" runat="server" Height="25" Width="125" placeholder="Fname"></asp:TextBox>  
  2. <asp:RequiredFieldValidator ID="ReqFldvalidator" runat="server" ValidationGroup="ValidGrp" ControlToValidate="txtFname" ErrorMessage="Enter the FirstName" ForeColor="Red"></asp:RequiredFieldValidator>  
  3. <asp:Button ID="BtnSubmit" runat="server" Height="25 " Width="125" Text="click" OnClick="BtnSubmit_Click" ValidationGroup="ValidGrp" />  

RangeValidator

 
The Rangevalidator control is verifies that the input value falls within a given range of number, date or string.
 
Example
  1. <asp:TextBox ID="txtage" runat="server" Height="25" Width="125" placeholder="Age"></asp:TextBox>  
  2. <asp:RangeValidator ID="RangeValidator" runat="server" ValidationGroup="ValidGrp" ControlToValidate="txtage" MinimumValue="18" MaximumValue="35" ForeColor="Red" ErrorMessage="Enter the 18-35 age only "></asp:RangeValidator>  
  3. <asp:Button ID="BtnSubmit" runat="server" Height="25 " Width="125" Text="click" OnClick="BtnSubmit_Click" ValidationGroup="ValidGrp" />  

RegularExpressionValidator

 
The RegularExpressionValidator control validates the input text of a control that matches a pattern defined by a RegularExpression.
 
Example
  1. <asp:TextBox ID="txtPhone" runat="server" Height="25" Width="125" placeholder="Phone"></asp:TextBox>  
  2. <asp:RegularExpressionValidator ID="RegExpValidator" runat="server" ValidationExpression="(0\d{1,4}-|\(0\d{1,4}\) ?)?\d{1,4}-\d{4}" ValidationGroup="ValidGrp" ControlToValidate="txtPhone" ForeColor="Red" ErrorMessage="Enter the valid phoneNo"></asp:RegularExpressionValidator>  
  3. <asp:Button ID="BtnSubmit" runat="server" Height="25 " Width="125" Text="click" OnClick="BtnSubmit_Click" ValidationGroup="ValidGrp" />  

CompareValidator

 
The Comaprevalidator control compares the value of one control to another using a comparison operator.
 
Example
  1. <asp:TextBox ID="txtNumber" runat="server" Height="25" Width="125" placeholder="Number "></asp:TextBox>  
  2. <asp:TextBox ID="txtCompareNumber" runat="server" Height="25" Width="125" placeholder="CompareNumber"></asp:TextBox>  
  3. <asp:CompareValidator ID="Cmpvalidator" runat="server" ValidationGroup="ValidGrp" ControlToValidate=" txtNumber " ValueToCompare=" txtCompareNumber" ForeColor="Red" ErrorMessage="Invalid Compare value"></asp:CompareValidator>  
  4. <asp:Button ID="BtnSubmit" runat="server" Height="25 " Width="125" Text="click" OnClick="BtnSubmit_Click" ValidationGroup="ValidGrp" />  

ValidationSummary

 
The ValidationSummary control doesn’t perform any validation. Instead, it shows a summary of errors raised by each control on the page.
 
Example
  1. <asp:ValidationSummary ID="VldSummry" runat="server" ValidationGroup="ValidGrp" ForeColor="Red" />  
Next Recommended Reading Range Validator In ASP.NET