CustomValidator Control Using ASP.NET In VB.NET

The CustomValidator control allows you to create a validation control with customized validation logic.
  • 4879
Download Files:
 

CustomValidator control

The CustomValidator control allows you to create a validation control with customized validation logic. The standard validator can not call a user-defined function. The CustomValidator control calls a user-defined function to perform validations. The interesting stuff in this control is that you have ability to validate your input data both from Client side using JavaScript and Server side by writing your code in code behind file in any of the .NET supported programming language.

Properties:

These are the some important properties of the CustomValidator control.

Text  - Text to display for the validator when the validated control is invalid.

BackColor - The background color of the RequiredFieldValidator control.

ControlToValidate - The id of the control to validate.

Display - The display behavior for the validation control. Legal values are:

-None (the control is not displayed. Used to show the error message only in the ValidationSummary control).

-Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.

-Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation.

ErrorMessage - The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set.

ForeColor - The foreground color of the control.

For example:

Drag a Label control, one TextBox control, Button control from the toolbox on the form. The form looks like this.

customvalidator.gif
 

Figure1.

Now click on the source button of the design form to see the .aspx code.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb"Inherits="WebApplication13.WebForm1" %>

<!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>

      <asp:Label ID="Label1" runat="server" Text="Enter a username: " />

<asp:TextBox id="txt1" runat="server" />

        <asp:CustomValidator ID="CustomValidator1"

ControlToValidate="txt1" Text="A username must be between 6 and 10 characters!"

runat="server" ForeColor="#FF5050"/><br />

        <br />

<asp:Button ID="Button1" Text="Submit" runat="server" style="height: 26px"/>

<br />
 

        <br />

    </div>

    </form>

</body>

</html
 

Now double click on the custom validate control of the design form and add the following code on the server validate event.

Protected Sub CustomValidator1_ServerValidate(ByVal source As ObjectByVal args AsSystem.Web.UI.WebControls.ServerValidateEventArgsHandles CustomValidator1.ServerValidate

        If Len(args.Value) < 6 Or Len(args.Value) > 10 Then

            args.IsValid = False

        Else

            args.IsValid = True

        End If

    End Sub

Now save and run the application and enter three character.

customvalidator1.gif
 

Figure2

Now click on the Button control.

customvalidator2.gif
 

Figure3

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.