The CustomValidator Server Control in asp.net


The CustomValidator Server Control:

So far, you have seen a wide variety of validation controls that are at your disposal. In many cases, these validation controls address many of the validation rules that you want to apply to your Web forms. Sometime, however, none of these controls work for you, and you have to go beyond what they offer. This is where the CustomValidator control comes into play.

The CustomValidator control allows you to build your own client-side or server-side validations that can then be easily applied to your Web forms.

Using Client-Side Validation

One of the worthwhile functions of the CustomValidator control is its capability to easily provide custom client-side validations. Many developers have their own collections of JavaScript functions they employ in their applications, and using the CustomValidator control is one easy way of getting these functions implemented.

Using the CustomValidator control to perform client-side validations

<%@ Page Language="C#" %>
<script runat="server">
    
    protected void Button1_Click(Object sender, EventArgs e) {
    Label1.Text = "VALID NUMBER!";
    }
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml&quot; >
<head id="Head1" runat="server">
    <title>CustomValidator</title>
    
    <script language="JavaScript">
        function validateNumber(oSrc, args) {
            args.IsValid = (args.Value % 5 == 0);
        }
    </script>
    </head>
<body>
    <form id="form2" runat="server">
    <div>
     <p>
      Number:
      <asp:TextBox ID="TextBox1"
       Runat="server"></asp:TextBox>
       &nbsp;
      <asp:CustomValidator ID="CustomValidator1"
       Runat="server" ControlToValidate="TextBox1"
       ErrorMessage="Number must be divisible by 5"
       ClientValidationFunction="validateNumber">
      </asp:CustomValidator>
     </p>
     <p>
      <asp:Button ID="Button1" OnClick="Button1_Click"
       Runat="server" Text="Button"></asp:Button>
     </p>
     <p>
      <asp:Label ID="Label1" Runat="server"></asp:Label>
     </p>
    </div>
    </form>
</body>
</html>

 If you run this page and make an invalid entry, you produce the result illustrated as below:

Using Server-Side Validation

If you create your own server-side validations, you can make them as complex as your applications require. For instance, using the CustomValidator for server-side validations is something you do if you want to check the user’s input against dynamic values coming from XML files, databases, or elsewhere.

Using the CustomValidator control to perform server-side validations

<%@ Page Language="C#" %>
<script runat="server">
    
    protected void Button1_Click(Object sender, EventArgs e) {
      if (Page.IsValid) {
      Label1.Text = "VALID ENTRY!";
      }
    }
    void ValidateNumber(object source, ServerValidateEventArgs args)
    {
    try
    {
       int num = int.Parse(args.Value);
       args.IsValid = ((num%5) == 0);
    }
    catch(Exception ex)
    {
       args.IsValid = false;
    }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml&quot; >
<head id="Head1" runat="server">
    <title>CustomValidator</title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
     <p>
      Number:
      <asp:TextBox ID="TextBox1"
       Runat="server"></asp:TextBox>
       &nbsp;
      <asp:CustomValidator ID="CustomValidator1"
       Runat="server" ControlToValidate="TextBox1"
       ErrorMessage="Number must be divisible by 5"
       OnServerValidate="ValidateNumber"></asp:CustomValidator>
     </p>
     <p>
      <asp:Button ID="Button1" OnClick="Button1_Click"
       Runat="server" Text="Button"></asp:Button>
     </p>
     <p>
      <asp:Label ID="Label1" Runat="server"></asp:Label>
     </p>
    </div>
    </form>
</body>
</html>

---------------===========------------

Other Related Posts:

Validations:

ASP.NET Validation Server Controls

The RequiredFieldValidator Server Control in asp.net

Validating Drop-Down Lists with the RequiredFieldValidator Control

The CompareValidator Server Control in asp.net

The RangeValidator Server Control in asp.net

The RegularExpressionValidator Server Control in asp.net

The CustomValidator Server Control in asp.net

The ValidationSummary Server Control in asp.net

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s