Validation Controls in ASP.Net

  1. RequiredFiledValidator
  2. RangeValidator
  3. CompareValidator
  4. RegularExpressionValidator
  5. CustomValidator
  6. Validation Summary
1. RequiredFiledValidator: Makes an input control a required field. Example
    <asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidatorEmailId" runat="server" ControlToValidate="txtEmailId"
        ErrorMessage="Email Id is Required" ValidationGroup="Submit"></asp:RequiredFieldValidator>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="Submit" />
2. RangeValidator: Checks that the user enters a value that falls between two values. Example
    <asp:TextBox ID="txtWorkingDays" runat="server"></asp:TextBox>
    <asp:RangeValidator ID="RangeValidatorWorkingDays" runat="server" ControlToValidate="txtWorkingDays"
        MaximumValue="7" MinimumValue="1" ErrorMessage="Working day must be between 1 to 7"
        ValidationGroup="Submit"></asp:RangeValidator>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="Submit" />
  
3. CompareValidator: Compares the value of one input control to the value of another input control or to a fixed value. Example
    <asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtEmailIdReEnter" runat="server"></asp:TextBox>
    <asp:CompareValidator ID="CompareValidatorEmailId" runat="server" ControlToCompare="txtEmailId"
        ControlToValidate="txtEmailIdReEnter" ErrorMessage="Email Id and Re Enter Email Id must be same"
        ValidationGroup="Submit"></asp:CompareValidator>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="Submit" />
4. RegularExpressionValidator: The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern. Example
    <asp:TextBox ID="txtPinCode" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidatorPindCode" runat="server"
        ControlToValidate="txtPinCode" ValidationExpression="\d{6}" ErrorMessage="The pin code must be 6 numeric digits!"
        ValidationGroup="Submit" />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="Submit" />
5. CustomValidator: The CustomValidator control allows you to write a method to handle the validation of the value entered. Example
Client Side Validation:
<asp:TextBox ID="txtPinCode" runat="server"></asp:TextBox>
    <asp:CustomValidator ID="CustomValidatorPinCode" runat="server" ControlToValidate="txtPinCode"
        ClientValidationFunction="validate" ErrorMessage="Name  must be more than 2"
        ValidationGroup="Submit"></asp:CustomValidator>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="Submit" />
<script type="text/javascript">
    function validate(sender, e) {
        var ctrl = document.getElementById('<%= txtPinCode.ClientID %>');
        e.IsValid = false;
        if (ctrl.value.length == 6) {
            e.IsValid = true;
        }
    }
</script>
Server Side Validation:

<asp:TextBox ID="txtPinCode" runat="server"></asp:TextBox>
    <asp:CustomValidator ID="CustomValidatorPinCode" runat="server" ControlToValidate="txtPinCode"
        OnServerValidate="validate" ErrorMessage="No of characters must be between 5 and 8"
        ValidationGroup="Submit"></asp:CustomValidator>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="Submit" />
    protected void validate(object sender, ServerValidateEventArgs e)
    {
        if (e.Value.Length == 6)
        {
            e.IsValid = true;
        }
        else
        {
            e.IsValid = false;
        }
    }

6. Validation Summary: The ValidationSummary control is used to display a summary of all validation errors occurred in a Web page. Example

    <asp:TextBox ID="txtPinCode" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtPinCode"
        ErrorMessage="Email Id is mandatory" Text="*" ValidationGroup="Submit"></asp:RequiredFieldValidator>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true" ValidationGroup="Submit" />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="Submit" />