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

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


<!DOCTYPE html>


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

<head runat="server">

    <title></title>

    <style type="text/css">

        .auto-style1{

            width:100%;

        }

        .auto-style2 {

            width: 185px;

        }

    </style>

</head>

<body>

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

        <div>

            <table cellpadding="4" cellspacing="4" class="auto-style1">

                <tr>

                    <td class="auto-style2">ENTER EVEN NUMBER</td>

                    <td>

                        <asp:TextBox ID="TextBox1" runat="server" Width="195px"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="Please Enter a number" ControlToValidate="TextBox1" SetFocusOnError="true"></asp:RequiredFieldValidator>

                        <%--Custom Validator is used when we want to apply our own logic--%>

                        <asp:CustomValidator ID="CustomValidator1" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="Please enter even number" ControlToValidate="TextBox1" SetFocusOnError="true" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>

                    </td>

                </tr>

                <tr>

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

                    <td>

                        <asp:Button ID="Button1" runat="server" Text="SUBMIT"/>

                    </td>

                </tr>

            </table>

        </div>

    </form>

</body>

</html>

Above File is ANOTHER_EXAMPLE_CV_PRACTICE\WebForm1.aspx













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

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

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            int value = int.Parse(args.Value);
            if (value % 2 == 0)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
}
Above File is ANOTHER_EXAMPLE_CV_PRACTICE\WebForm1.aspx.cs






Comments

Popular posts from this blog

AJAX Method Of jQuery AJAX In ASP.NET Web Forms | Learn ASP.NET Web Forms

List Box In ASP.NET Web Forms | ASP.NET ListBox | Web Forms Tutorial | ASP.NET

Display Auto Increment Value In Text Field After Insert To Database ASP.NET