Using Validation Control Event Using ASP.NET In VB.NET

In this article you learn about CustomValidator and how to implement custom validation control Event.
  • 1949
 

Introduction
 
Custom validator performed user defined validation on an input control. This control allows you to define your own validation. the property ClientValidationFunction sets the name of function or script that will do the validation. This function takes two parameters. The first argument source identifies the source control to validate. The second argument hold the data to validate. The custom validators does not checks for whether there is some input in the control to be validated so you must need to attach a RequiredFieldValidator in addition to a CustomValidator control, if you require that certain data should be present in the validated field. So you must provide a server side validation function for a CustomValidator control. Here we are discussing an example in which we are implementing custom validation control Event.


Getting Started

  • Simply create a new ASP.NET web application 
  • Drag a TextBox, Button, CustomValidator and a Label control on your web page. The page will look like below.

    custom1.gif
     
  • Then add the below code.

    <%@ Page Language="VB" %>
    <script runat="server"> 
       
    Protected Sub valProductCode_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
           
    If args.Value.Length = 4 Then
                args.IsValid = True
            Else
                args.IsValid = False
            End If
        End Sub
    </
    script>
    <
    html xmlns="http://www.w3.org/1999/xhtml" >
    <
    head id="Head1" runat="server">
        <title>Show Validate Empty Text</title>
    </
    head>
    <
    body>
        <form id="form1" runat="server">
        <div>
       
       
    <asp:Label 
           
    id="lblProductCode"
            Text="Product Code:"
            AssociatedControlID="txtProductCode"
            Runat="server" />
        <br />
        <asp:TextBox
            id="txtProductCode"
            Runat="server" />
        <asp:CustomValidator
            id="valProductCode"
            ControlToValidate="txtProductCode"
            Text="(Invalid product code)"
            ValidateEmptyText="true"
            OnServerValidate="valProductCode_ServerValidate"
           
    Runat="server" />
        <br /><br />
        <asp:Button
            id="btnSubmit"
            Text="Submit"
            Runat="server" />   
       
    </div>
        </form>
    </
    body>
    </
    html>
     
  • Now run your application.

Output:-

custom2.gif

custom3.gif

custom4.gif

custom5.gif

custom6.gif

Summary


In this article you learned how to implement custom validation control Event.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.