ARTICLE

ASP.NET CustomValidator Control in VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET August 04, 2011
The CustomValidator control allows you to create a validation control with customized validation logic.
Download Files:
 
Reader Level:

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 Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles 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

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor