Type of validation

 In ASP.NET Web Forms, you can use various validators to handle different types of input validation on the frontend. Below is an example demonstrating how to use `RequiredFieldValidator`, `RangeValidator`, `CompareValidator`, `RegularExpressionValidator`, `CustomValidator`, and `ValidationSummary` to validate fields such as phone number, email, PIN number, salary, Aadhaar card number, PAN card number, and employee code.


### Frontend Code (ASPX)


```asp

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidationExample.aspx.cs" Inherits="YourNamespace.ValidationExample" %>


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Validation Example</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please fix the following errors:" CssClass="error" />


            <!-- Phone Number -->

            <asp:TextBox ID="txtPhoneNumber" runat="server" />

            <asp:RequiredFieldValidator ID="rfvPhoneNumber" runat="server" ControlToValidate="txtPhoneNumber" ErrorMessage="Phone number is required." CssClass="error" />

            <asp:RegularExpressionValidator ID="revPhoneNumber" runat="server" ControlToValidate="txtPhoneNumber" ErrorMessage="Invalid phone number format." 

                ValidationExpression="\d{10}" CssClass="error" />


            <br />


            <!-- Email -->

            <asp:TextBox ID="txtEmail" runat="server" />

            <asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="Email is required." CssClass="error" />

            <asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="Invalid email format." 

                ValidationExpression="^[^@\s]+@[^@\s]+\.[^@\s]+$" CssClass="error" />


            <br />


            <!-- PIN Number -->

            <asp:TextBox ID="txtPinNumber" runat="server" />

            <asp:RequiredFieldValidator ID="rfvPinNumber" runat="server" ControlToValidate="txtPinNumber" ErrorMessage="PIN number is required." CssClass="error" />

            <asp:RegularExpressionValidator ID="revPinNumber" runat="server" ControlToValidate="txtPinNumber" ErrorMessage="Invalid PIN number format." 

                ValidationExpression="\d{6}" CssClass="error" />


            <br />


            <!-- Salary -->

            <asp:TextBox ID="txtSalary" runat="server" />

            <asp:RequiredFieldValidator ID="rfvSalary" runat="server" ControlToValidate="txtSalary" ErrorMessage="Salary is required." CssClass="error" />

            <asp:RangeValidator ID="rvSalary" runat="server" ControlToValidate="txtSalary" ErrorMessage="Salary must be between 1000 and 1000000." 

                MinimumValue="1000" MaximumValue="1000000" Type="Double" CssClass="error" />


            <br />


            <!-- Aadhaar Card Number -->

            <asp:TextBox ID="txtAadhaarCard" runat="server" />

            <asp:RequiredFieldValidator ID="rfvAadhaarCard" runat="server" ControlToValidate="txtAadhaarCard" ErrorMessage="Aadhaar card number is required." CssClass="error" />

            <asp:RegularExpressionValidator ID="revAadhaarCard" runat="server" ControlToValidate="txtAadhaarCard" ErrorMessage="Invalid Aadhaar card number format." 

                ValidationExpression="\d{12}" CssClass="error" />


            <br />


            <!-- PAN Card Number -->

            <asp:TextBox ID="txtPANCard" runat="server" />

            <asp:RequiredFieldValidator ID="rfvPANCard" runat="server" ControlToValidate="txtPANCard" ErrorMessage="PAN card number is required." CssClass="error" />

            <asp:RegularExpressionValidator ID="revPANCard" runat="server" ControlToValidate="txtPANCard" ErrorMessage="Invalid PAN card number format." 

                ValidationExpression="[A-Z]{5}[0-9]{4}[A-Z]{1}" CssClass="error" />


            <br />


            <!-- Employee Code -->

            <asp:TextBox ID="txtEmpCode" runat="server" />

            <asp:RequiredFieldValidator ID="rfvEmpCode" runat="server" ControlToValidate="txtEmpCode" ErrorMessage="Employee code is required." CssClass="error" />

            <asp:RegularExpressionValidator ID="revEmpCode" runat="server" ControlToValidate="txtEmpCode" ErrorMessage="Invalid employee code format." 

                ValidationExpression="[A-Za-z0-9]{5,10}" CssClass="error" />


            <br />


            <!-- Submit Button -->

            <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

        </div>

    </form>

</body>

</html>

```


### Backend Code (ASPX.CS)


In your code-behind file (`ValidationExample.aspx.cs`), you would handle the button click event or any other backend logic as needed:


```csharp

using System;

using System.Web.UI;


namespace YourNamespace

{

    public partial class ValidationExample : Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            // Page Load logic here

        }


        protected void btnSubmit_Click(object sender, EventArgs e)

        {

            if (Page.IsValid)

            {

                // Process the form data

                string phoneNumber = txtPhoneNumber.Text;

                string email = txtEmail.Text;

                string pinNumber = txtPinNumber.Text;

                string salary = txtSalary.Text;

                string aadhaarCard = txtAadhaarCard.Text;

                string panCard = txtPANCard.Text;

                string empCode = txtEmpCode.Text;


                // Example: Save to database or perform some action

            }

        }

    }

}

```


### Explanation


1. **RequiredFieldValidator**: Ensures the field is not empty.

2. **RegularExpressionValidator**: Validates the field against a regular expression. Useful for formats like phone numbers and email addresses.

3. **RangeValidator**: Checks that a numeric value falls within a specified range.

4. **CompareValidator**: Compares the value of one input control to another or to a fixed value.

5. **CustomValidator**: Allows for custom validation logic if needed.

6. **ValidationSummary**: Displays a summary of validation errors.


Make sure you have the proper namespaces and any necessary error handling in place. You can also customize the CSS classes (`error` in this case) to style the validation messages.

Post a Comment

0 Comments