Why Do We Need Validation Control in ASP.NET

Hi Guy’s, Here is the article for validation control in asp.net. why do we need validation control in asp.net. Validation control used to validate the input given by user, whether it is authentic/correct or not. without using validation user can input blank or null value.that’s not correct idea to store null information of user into the database.so we must include validation control. There are 7 validation control uses in asp.net. 1.Pointer 2.CompareValidator 3.CustomValidator 4.RangeValidator 5.RegularExpressionValidator 6.ValidationSummary.

Compare Validator Control
It is used to compare the value of an input control against a value of another input control.

RequiredField Validator Control
It is used to make a control required.

Range Validator
This control validate input value and assure that it is within the predefined range.

RegularExpression Validator
This control matches input value with a predefined regular expression’s pattern and verify whether input is valid or not.

ValidationSummary
This control is used to display all the errors on the web page in summary form.

Without Using Validation Control. it stores null values into database. see below image.

After using validation control . User must enter values into the control. without entering values.errors message comes out. see below image.

Password and Confirm password require because i used compare validator control. see below image.

Example of validation control. See the down codes. copy and paste. use into Visual Studio.

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

<!DOCTYPE html>

<html>
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            font-size: xx-large;
            color: #003399;
        }
        .auto-style3 {
            width: 401px;
        }
        .auto-style4 {
            background-color: #0099CC;
        }
        .auto-style5 {
            background-color: #33CCCC;
        }
        .auto-style6 {
            height: 30px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table class="auto-style1">
                <tr>
                    <td>
                        <table class="auto-style1">
                            <tr>
                                <td class="auto-style2"><strong>-------------------Register---------------</strong></td>
                            </tr>
                            <tr>
                                <td>Create Your Account.Its Free and Only Takes a Minute</td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
        <table class="auto-style1">
            <tr>
                <td class="auto-style3">
                    <asp:Label ID="Label1" runat="server" Text="Enter First Name"></asp:Label>
                    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtFirstName" ErrorMessage="Enter Your Name" ForeColor="#FF3300"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Enter Last Name"></asp:Label>
                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtLastName" ErrorMessage="Enter Last Name" ForeColor="#FF3300"></asp:RequiredFieldValidator>
                </td>
            </tr>
        </table>
        <table class="auto-style1">
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Enter Email"></asp:Label>
                    <asp:TextBox ID="txtEmail" runat="server" TextMode="Email" Width="348px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtEmail" ErrorMessage="Enter Your Email" ForeColor="#FF3300"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style6">
                    <asp:Label ID="Label4" runat="server" Text="Enter Password"></asp:Label>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="316px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtPassword" ErrorMessage="Enter the Password" ForeColor="#FF3300"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label5" runat="server" Text="Enter Confirm Password"></asp:Label>
                    <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" Width="258px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtConfirmPassword" ErrorMessage="Enter Confirm Password" ForeColor="#FF3300"></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword" ControlToValidate="txtConfirmPassword" ErrorMessage="Password Must be Match" ForeColor="#FF3300"></asp:CompareValidator>
                </td>
            </tr>
        </table>
        <asp:CheckBox ID="CheckBox1" runat="server" CssClass="auto-style5" Text="I accept the Terms of Use &amp; Privacy Policy" />
        <table class="auto-style1">
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" CssClass="auto-style4" Height="50px" OnClick="Button1_Click1" Text="Submit" Width="306px" />
                    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

Happy Coding…Thanks.

Post Author: adama