List of Validation Controls in ASP.NET

toolbox

Validation Controls in ASP.NET is very interesting topic, the reason is by using these controls you can restrict users from entering invalid content.

ASP.NET validation controls provide two ways of validation: Server-side or Client-side. So as per our requirement we can choose either or both of them.

There are six validation controls available in ASP.NET Toolkit by default. If you want to use some other fancy validation controls, then you can download AJAX toolkit from CodePlexwebsite.

  • Required Field Validator
  • Compare Validator
  • Range Validator
  • Regular Expression Validator
  • Custom Validator
  • Validation Summary
Details with Syntax:

Required Field Validator: Checks whether control contains data.
  1. <asp:RequiredFieldValidator ID="UrlPathRequired"   
  2. runat="server" ControlToValidate="UrlPath"   
  3. ErrorMessage="Please check URL">*</asp:RequiredFieldValidator>  
Compare Validator: Comparison between two controls data.
  1. <asp:CompareValidator ID="CompareValidatorPassword" runat="server"  
  2. ErrorMessage="Password do not match">  
  3. </asp:CompareValidator>  
Range Validator: Checks if the data to be validated falls between the specified ranges.
  1. <asp:RangeValidator ID="RangeValidatorAge" runat="server"  
  2. ControlToValidate="txtAge" ErrorMessage="Enter your age (18 - 25)"   
  3. MaximumValue="25" MinimumValue="18" Type="Integer">  
  4. </asp:RangeValidator>  
Regular Expression Validator: Email Format,Phone Format etc.
  1. <asp:RegularExpressionValidator ID="URLPathExpressionValidator" runat="server"  
  2. ControlToValidate="UrlPath" ErrorMessage="Invalid URL Path"  
  3. ValidationExpression="string" ValidationGroup="string">Invalid URL  
  4. </asp:RegularExpressionValidator>  
Custom Validator: Checks the validity of an entered items through a client side script or server side code.
  1. <asp:CustomValidator ID="URLPathCustomValidator" runat="server"  
  2. ErrorMessage="URL Path exists" ControlToValidate="UrlPath"   
  3. OnServerValidate="UrlPathCustomValidator_ServerValidate">  
  4. URL is already used by another application or folder  
  5. </asp:CustomValidator>  
Validation Summary: It doesn’t performs any validations. However, displays a summary of validation errors.
  1. <asp:ValidationSummary ID="ValidationSummaryForm" runat="server"  
  2. DisplayMode = "BulletList" ShowSummary = "true"   
  3. HeaderText="Form contains below Errors:" />  
Hope you found this post useful and worth. Keep watching this space & stay connected for more stuffs on ASP.NET. For more such articles, please follow my blog.

What do you think?

Dear Reader,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.
Happy Learning.