Create user control based on CustomValidator : CustomValidator « Validation « ASP.NET Tutorial






<%@ Page Language="VB" %>
<%@ Register TagPrefix="Java2sASP" TagName="LoginForm" src="Control.ascx" %>

<html><body>
   <form runat="server">
      <Java2sASP:LoginForm id="LoginForm1" runat="server"/>
   </form>
   
   <asp:Label id="lblMessage" runat="server" />
</body></html>


File: Control.ascx

<script runat="server">
   sub Submit(Sender as Object, e as EventArgs)
      if Page.IsValid then
         'do something
      end if
   end sub

   sub ValidateThis(Sender as Object, args as _
   ServerValidateEventArgs) 
      if len(args.Value) < 4 then
         args.IsValid = false
      else
         args.IsValid = true
      end if
   end sub
</script>

      <asp:Label id="lblMessage" runat="server" />
      <table>
      <tr>
         <td valign="top">Username:</td>
       <td valign="top">
            <asp:Textbox id="tbUserName" runat="server"/><br>
            <asp:CustomValidator runat="server"
               OnServerValidate="ValidateThis" 
               OnClientValiate="validateLength" 
               Display="Dynamic"
               ControlToValidate="tbUserName"
               ErrorMessage="The username must be 4 characters or longer"/>
       </td>
    </tr>
    <tr>
         <td valign="top">Password:</td>
       <td valign="top">
          <asp:Textbox id="tbPassword" runat="server" TextMode="password" />
       </td>
    </tr>
    <tr>
         <td align="right">
          <ASP:Button id="tbSubmit" runat="server" OnClick="Submit" text="Submit" />
       </td>
    </tr>
      </table>








8.3.CustomValidator
8.3.1.You can associate a custom validation function with the CustomValidator control.
8.3.2.Validate a blank field
8.3.3.Performing validation against no particular field.
8.3.4.Use both client side and server side script to validate (VB.net)
8.3.5.Create user control based on CustomValidator
8.3.6.Using the CustomValidator control to perform client-side validations
8.3.7.Using the CustomValidator control to perform server-side validations (C#)
8.3.8.Using the CustomValidator control to perform server-side validations (VB)
8.3.9.Custom validator with Javascript
8.3.10.CustomValidator for login page
8.3.11.CustomValidator for login page (VB)