Learn About Validation Controls In ASP.NET

Introduction

 
Hey guys, in this article we will discuss validation controls in ASP.NET. In the following article, we will see all validation controls with examples. I hope that you like this article.
 

Types Of Validation Control In ASP.NET

 
The following list shows the six types of validation control in ASP.NET:
  1. Required Field Validator
  2. Compare Validator
  3. Range Validator
  4. Regular Expression Validator
  5. Custom Validator
  6. Validation Summary

Required Field ValidatorControl

 
As per the name suggests, this validation control is used to show error message when a user does not enter a value field that is marked as required. You can only define one required field validation control to only one input field. The following are important properties of RequiredFieldValidator:
  1. ControllToValidate
    This property is required for RequiredFieldValidator control. In this property, you have to pass the Id of an input that you want to make required.

  2. Id
    This property defines a unique id for RequiredFieldValidator control. When you add RequiredFieldValidator to your page it automatically takes Id. 

  3. ErrorMessage
    In this property you can pass string message which show to user when RequiredFieldValidator control return false means when the user does not enter a value in the input which is marked as required.
  1. <div class="form-horizontal">    
  2.         <h4>Validation Example</h4>    
  3.         <hr />    
  4.         <div class="form-group">    
  5.             <asp:Label ID="successMsg" runat="server" Text="" CssClass="text-success"></asp:Label>    
  6.             <br />    
  7.             <asp:Label runat="server" AssociatedControlID="textboxValue" CssClass="col-md-2 control-label">    
  8.                 Required Fied Validation</asp:Label>    
  9.             <div class="col-md-10">    
  10.                 <asp:TextBox runat="server" ID="textboxValue" CssClass="form-control" />    
  11.                 <asp:RequiredFieldValidator runat="server" ControlToValidate="textboxValue"    
  12.                     CssClass="text-danger" ErrorMessage="The field is required.This is error message which given by required field validation" />    
  13.             </div>    
  14.         </div>    
  15.     
  16.         <div class="form-group">    
  17.             <div class="col-md-offset-2 col-md-10">    
  18.                 <asp:Button runat="server" OnClick="CreateUser_Click" Text="Required Field Validation" CssClass="btn btn-default" />    
  19.             </div>    
  20.         </div>    
  21.     </div>    

Compare Validator Control

 
As per the name defines, this control is used for comparing two input values. This control is not usually used, but when you need to compare two input fields, then you can use it.
 
CompareValidator is mostly used to compare passwords at registration time. CompareValidator returns false when your predefine two input values that do not match. Following are some important properties of CompareValidator.
  1. Id
    This property defines a unique id for CompareValidator control. When you add CompareValidator in your page it automatically takes Id.

  2. ControllToValidate
    This property is required for CompareValidator control. In this property, you have to pass the id of input fields that you want to compare.

  3. ControlToCompare
    This property is also required for CompareValidator. In this property, you have to pass the Id of other input fields that you want to compare with the value input field that you gave in the ControlToValidate property.

  4. ErrorMessage
    In this property, you can pass string message which show to user when CompareValidator control return false means when user does not enter same value in both field then CompareValidator return error message.
  1. <div class="form-horizontal">    
  2.         <h4>Validation Example</h4>    
  3.         <hr />    
  4.         <div class="form-group">    
  5.             <asp:Label runat="server" AssociatedControlID="txt1" CssClass="col-md-2 control-label">    
  6.                 Textbox 1</asp:Label>    
  7.             <div class="col-md-10">    
  8.                 <asp:TextBox runat="server" ID="txt1" CssClass="form-control" />    
  9.             </div>    
  10.         </div>    
  11.         <div class="form-group">    
  12.             <asp:Label runat="server" AssociatedControlID="txt2" CssClass="col-md-2 control-label">    
  13.                 Textbox 2</asp:Label>    
  14.             <div class="col-md-10">    
  15.                 <asp:TextBox runat="server" ID="txt2" CssClass="form-control" />    
  16.                 <asp:CompareValidator    
  17.                     ID="CompareValidator1"    
  18.                     runat="server"    
  19.                     ErrorMessage="Both Value Must be same "    
  20.                     CssClass="text-danger"    
  21.                     ControlToCompare="txt1"    
  22.                     ControlToValidate="txt2"></asp:CompareValidator>    
  23.             </div>    
  24.         </div>    
  25.     
  26.     
  27.     </div>   

Range Validator Control

 
The RangeValidator Control does exactly what the name implies, it makes sure that the user input value within a specified range. You can validate a number, string, dates, and also currency. You have to define a minimum and maximum value for RangeValidator control. When the user enters a value out of the given range, then RangeValidator control returns an error message. Following are some important properties of RangeValidator control.
  1. Id
    This property gives a unique identity to RangeValidator control. When you add RangeValidator in your page it by default takes a unique id.

  2. ErrorMessage
    This property takes a string parameter which shows when RangeValidator control returns false.

  3. ControlToValidate
    This property is required for RangeValidator control. In this property, you have to pass the Id of the input field that you want to validate.

  4. MaximumValue
    In this property, you have to pass a maximum value for validation. Users cannot enter a value greater than this value.

  5. MinimumValue
    In this property, you have pass a minimum value for validation. Users cannot enter a value less than this value.

  6. Type
    In This property you have to define type of value which pass in input. This property value like integer, currency , date, double and string.
  1. <div class="form-horizontal">    
  2.        <h4>Range Validation Example</h4>    
  3.        <hr />    
  4.        <div class="form-group">    
  5.            <asp:Label runat="server" AssociatedControlID="txt1" CssClass="col-md-2 control-label">    
  6.                Textbox 1</asp:Label>    
  7.            <div class="col-md-10">    
  8.                <asp:TextBox runat="server" ID="txt1" CssClass="form-control" />    
  9.                <asp:RangeValidator    
  10.                    ID="RangeValidator1"    
  11.                    runat="server"    
  12.                    ErrorMessage="Range must be within 1 to 100"    
  13.                    CssClass="text-danger"    
  14.                    ControlToValidate="txt1"    
  15.                    Type=""    
  16.                    MaximumValue="100"    
  17.                    MinimumValue="1"></asp:RangeValidator>    
  18.            </div>    
  19.        </div>    
  20.    </div>  

Regular Expression Validator Control

 
RegularExpressionValidator control is used when you have to get data from the user in certain predefine expressions, like email address, website URL, pin code, etc. In RegularExpressionValidator control, you have to pass expression which compares with a user input. If the expression and user input match, then it returns true, otherwise it return false and shows an error message. Using Regular Expression Validator is very simple. Simply set the ValidationExpression property to any type of expression you want and it will validate it. The following are some important properties of RegularExpressionValidator control.
  1. Id
    This property gives a unique identity to RegularExpressionValidator control. When you add RegularExpressionValidator in your page it by default takes a unique id.

  2. ErrorMessage
    This property takes a string parameter which shows when RegularExpressionValidator control returns false.

  3. ControlToValidate
    This property is required for RegularExpressionValidator control. In this property, you have to pass the Id of an input field that you want to validate.

  4. Validationexpression
    You have to pass your expression in this property.
  1. <div class="form-horizontal">    
  2.        <h4>Regular Expression Validation Example</h4>    
  3.        <hr />    
  4.        <div class="form-group">    
  5.            <asp:Label runat="server" AssociatedControlID="TextBox1" CssClass="col-md-2 control-label">    
  6.                Textbox</asp:Label>    
  7.            <div class="col-md-10">    
  8.                <asp:TextBox runat="server" ID="TextBox1" CssClass="form-control" />    
  9.                <asp:RegularExpressionValidator    
  10.                    ID="RegularExpressionValidator1"    
  11.                    CssClass="text-danger"    
  12.                    ControlToValidate="TextBox1"    
  13.                    ValidationExpression="^[0-9]{4}$"    
  14.                    runat="server" ErrorMessage="Regular expression example . Character must be 4 or more."></asp:RegularExpressionValidator>    
  15.            </div>    
  16.        </div>    
  17.     
  18.    </div>    

Custom Validator Control

 
ASP.NET has many Validator, as we see above, but if you have to validate some input and you need another validator other than these, then you can create your own validator control. CustomValidator allows you to create your own validator control. CustomValidator can be used both client side and also server side. In this article, we used server side validation, where the user must enter a character of more than five. The following are some important properties of CustomValidator.
  1. Id
    This property is given a unique identity to CustomValidator control. When you add CustomValidator on your page, by default it takes a unique id.

  2. ErrorMessage
    This property takes a string parameter which shows when CustomValidator control returns false.

  3. ControlToValidate
    This property is required for CustomValidator control. In this property, you have to pass the Id of the input field that you want to validate.

  4. OnServerValidate
    With this property you have to pass a function name that you want to use for creating this validator. You have to create this function on an .aspx.cs file.
  1. <div class="form-horizontal">    
  2.        <h4>Custom Validation Example</h4>    
  3.        <hr />    
  4.        <div class="form-group">    
  5.            <asp:Label runat="server" AssociatedControlID="TextBox1" CssClass="col-md-2 control-label">    
  6.                Textbox</asp:Label>    
  7.            <div class="col-md-10">    
  8.                <asp:TextBox runat="server" ID="TextBox1" CssClass="form-control" />    
  9.                <asp:CustomValidator ID="CustomValidator1" runat="server" C    
  10.                    ontrolToValidate="TextBox1" CssClass="text-danger"    
  11.                    OnServerValidate="CustomValidator"    
  12.                    ErrorMessage="Textbox value size must be 5 or more character "></asp:CustomValidator>    
  13.            </div>    
  14.        </div>    
  15.        <div class="form-group">    
  16.            <div class="col-md-offset-2 col-md-10">    
  17.                <asp:Button runat="server" Text="Submit" CssClass="btn btn-default" />    
  18.            </div>    
  19.        </div>    
  20.    </div>    
  1. protected void CustomValidator(object source, ServerValidateEventArgs args)  
  2. {  
  3.         if (TextBox1.Text.Length >= 5)  
  4.             args.IsValid = true;   
  5.         else  
  6.             args.IsValid = false;  
  7. }  

Validation Summary Control

 
This is an additional control that used to show error message. The ValidationSummary control is used to show all error messages in one place. ValidationSummary shows an error message of all invalid fields in the list. The following are some important properties of ValidationSummary.
  1. Id
    This property is given a unique identity to ValidationSummary control. When you add ValidationSummary in your page it by default takes a unique Id.

  2. HeaderText
    This property takes string value and show that string as a header of list.
  1. <div class="form-horizontal">    
  2.        <h4>Validation Summary Example</h4>    
  3.        <hr />    
  4.        <asp:ValidationSummary ID="ValidationSummary1" HeaderText="" CssClass="text-danger" runat="server" />    
  5.        <div class="form-group">    
  6.            <asp:Label runat="server" AssociatedControlID="TextBox1" CssClass="col-md-2 control-label">    
  7.                Regular Expression Validation </asp:Label>    
  8.            <div class="col-md-10">    
  9.                <asp:TextBox runat="server" ID="TextBox1" CssClass="form-control" />    
  10.                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" CssClass="text-danger" ControlToValidate="TextBox1" ValidationExpression="^[0-9]{4}$" runat="server" ErrorMessage="Regular expression example . Character must be 4 or more."></asp:RegularExpressionValidator>    
  11.            </div>    
  12.        </div>    
  13.        <div class="form-group">    
  14.            <asp:Label runat="server" AssociatedControlID="TextBox2" CssClass="col-md-2 control-label">    
  15.                Required Field Validation </asp:Label>    
  16.            <div class="col-md-10">    
  17.                <asp:TextBox runat="server" ID="TextBox2" CssClass="form-control" />    
  18.                <asp:RequiredFieldValidator    
  19.                    ID="RequiredFieldValidator1"    
  20.                    ControlToValidate="TextBox2"    
  21.                    runat="server"    
  22.                    ErrorMessage="This Field Is Required"    
  23.                    CssClass="text-danger"></asp:RequiredFieldValidator>    
  24.            </div>    
  25.        </div>    
  26.        <div class="form-group">    
  27.            <div class="col-md-offset-2 col-md-10">    
  28.                <asp:Button runat="server" Text="Submit" CssClass="btn btn-default" />    
  29.            </div>    
  30.        </div>    
  31.    </div>    

Conclusion

 
In this article, we learned about the various types of validator control in ASP.NET. If you found this article helpful, kindly share it with your friends. If you have any questions or suggestions about this article, please comment below. Thank you!


Similar Articles