I have one custom validator, one textbox & one button..I m checking the length of data entered in textbox.

VB CODE

<asp:CustomValidator ID="CustomValidator1" runat="server"  OnServerValidate="ValidateThis"
 ErrorMessage="The username must be 8" ControlToValidate="TextBox4"></asp:CustomValidator></td>
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <script runat="server">
      
      Sub ValidateThis(ByVal Sender As Object, ByVal args As _
       ServerValidateEventArgs)
          If Len(args.Value) < 8 Then
              args.IsValid = False
          Else
              args.IsValid = True
          End If
      End Sub
    </script>


<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

When i click on button,page is postbacked,if the length is less than 8 error msg comes,else nothing.

Mine C# Equivalent code-

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat ="server" >
void ValidateThis(object Sender,ServerValidateEventArgs args)
{
    if(len(args.Value)< 0)
    {
        args.IsValid = false; 
    }
                else
          args.IsValid = true ; 
        }
  </script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">

ERROR - The name 'len' does not exist in the current context

<script runat="server">
void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length < 8)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }

    }
      
    </script>

replace your Script with the above code
try this ......
Chithra

very very thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.