CustomValidator Server side validation example in ASP.NET with C#

CustomValidator Server side validation example in ASP.NET 

In this example, we are going to learn the CustomValidator (server side validation controls).

Background: some times need to do validation for some controls from server side only. Some times if user has disable javascript for the browser, we required validation from the server side. Due to this reason into consideration Microsoft has provided CustomValidator validation control to us.

CustomValidator required ControlToValidate, id, runt at and error message. Along with this we required to fire one server side event i.e onservervalidate. At the time of form submission, this event will fire and check for the value is correct or not. and then form submission action will perform.

For doing this one we have to write one method in the code behind page i.e .cs file. After running below aspx and .cs file code you will complete the Custom Validation from server side.

This is one of the good ASP.NET Interview Question

Output:
CustomValidator Server side validation example in ASP.NET with C#
CustomValidator Server side validation example in ASP.NET with C#


ASP.NET CODE

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomValidator_ServerSide.aspx.cs" Inherits="CustomValidator_ServerSide" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table style="width:40%;">
            <tr>
                <td>
                    <b>Please enter data</b></td>
                <td>
                    <asp:TextBox ID="txtData" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" 
                        ControlToValidate="txtData" ErrorMessage="Length should be 8 characters" 
                        onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnLength" runat="server" Text="Validate length" />
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>



C# Code(page behind code) :

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class CustomValidator_ServerSide : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { try { if (args.Value.Length < 8) { args.IsValid = false; } else { args.IsValid = true; } } catch (Exception ex) { Response.Write(ex.Message); } } } 
 

Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar