Validator Control In ASP.NET Web forms | Form Validation | ASP.NET WebForms

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


<!DOCTYPE html>


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

<head runat="server">

    <title></title>

    <style type="text/css">

        .auto-style1 {

            width: 89%;

        }

        .auto-style2 {

            height: 23px;

        }

        .auto-style3 {

            height: 23px;

            width: 148px;

        }

        .auto-style4 {

            width: 148px;

        }

    </style>

</head>

<body>

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

        <div>

            <h2>STUDENTS REGISTRATION FORM</h2>

           

            <br />

            <table class="auto-style1">

                <tr>

                    <td colspan="2">

                        <asp:ValidationSummary ID="ValidationSummary1" runat="server" BackColor="Silver" ForeColor="Red" Font-Size="Large"/>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style3">STUDENT NAME</td>

                    <td class="auto-style2">

                        <asp:TextBox ID="NameTextBox" runat="server" Width="216px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter Name" ForeColor="Red" ControlToValidate="NameTextBox" SetFocusOnError="true" Text="*"></asp:RequiredFieldValidator>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style4">STUDENT EMAIL</td>

                    <td>

                        <asp:TextBox ID="EmailTextBox" runat="server" Width="216px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Enter Email" ForeColor="Red" ControlToValidate="EmailTextBox" Display="Dynamic" Text="*"></asp:RequiredFieldValidator>

                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Email is Invalid" ForeColor="Red" ControlToValidate="EmailTextBox" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" Text="*"></asp:RegularExpressionValidator>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style4">RE-ENTER EMAIL</td>

                    <td>

                        <asp:TextBox ID="ReEnterEmailTextBox" runat="server" Width="216px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please Re-Enter Email" ForeColor="Red" ControlToValidate="ReEnterEmailTextBox" Display="Dynamic" Text="*"></asp:RequiredFieldValidator>

                        <asp:CompareValidator ID="CompareValidator" runat="server" Display="Dynamic" ErrorMessage="Email is not identical" ForeColor="Red" ControlToCompare="EmailTextBox" ControlToValidate="ReEnterEmailTextBox" SetFocusOnError="true" Text="*"></asp:CompareValidator>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style4">CLASS</td>

                    <td>

                        <asp:TextBox ID="ClassTextBox" runat="server" Width="216px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please Enter Class" ForeColor="Red" ControlToValidate="ClassTextBox" Display="Dynamic" Text="*" SetFocusOnError="true"></asp:RequiredFieldValidator>

                        <asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Class Should be from 1 to 12" ForeColor="Red" Display="Dynamic" Text="*" ControlToValidate="ClassTextBox" SetFocusOnError="true" MaximumValue="12" MinimumValue="1" Type="Integer"></asp:RangeValidator>


                    </td>

                </tr>

                <tr>

                    <td class="auto-style4">FEES</td>

                    <td>

                        <asp:TextBox ID="FeesTextBox" runat="server" Width="216px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Please Enter Fees" ForeColor="Red" ControlToValidate="FeesTextBox" Display="Dynamic" Text="*" SetFocusOnError="true"></asp:RequiredFieldValidator>

                        <asp:RangeValidator ID="RangeValidator2" runat="server" ErrorMessage="Fees should be from 2000.00 to 5000.00" ForeColor="Red" ControlToValidate="FeesTextBox" Display="Dynamic" Text="*" SetFocusOnError="true" MaximumValue="5000.00" MinimumValue="2000.00" Type="Double"></asp:RangeValidator>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style4">GENDER</td>

                    <td>

                        <asp:RadioButton ID="MaleRadioButton" runat="server" Text="Male" GroupName="GenderGroup"/>

                        <asp:RadioButton ID="FemaleRadioButton" runat="server" Text="Female" GroupName="GenderGroup"/>

                        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select a gender" ForeColor="Red" Text="*" Display="Dynamic" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>

                        

                    </td>

                </tr>

                <tr>

                    <td class="auto-style4">&nbsp;</td>

                    <td>

                        <asp:Button ID="Button1" runat="server" Text="SUBMIT" Height="36px" Width="105px" OnClick="Button1_Click"/>

                    </td>

                </tr>

            </table>

           

        </div>

    </form>

</body>

</html>

Above File is VALIDATION_CONTROLS\VALIDATION_FORM.aspx













using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace VALIDATION_CONTROLS
{
    public partial class VALIDATION_FORM : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(),"Scripts","<script>alert('YOUR FORM HAS BEEN SUBMITTED !!')</script>");
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (MaleRadioButton.Checked || FemaleRadioButton.Checked)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
}
Above File is VALIDATION_CONTROLS\VALIDATION_FORM.aspx.cs

Comments

Popular posts from this blog

Range Validator Control In ASP.NET Web forms | Form Validation | ASP.NET Web forms

Query String In ASP.NET Web Forms | State Management | ASP.NET | Web Forms

Validation Summary Control In ASP.NET Web forms | Form Validation | ASP.NET