Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello All,

I am trying to use a Custom Validator for the validation of the Length of the User Name. I am using VBScript as the scripting language.

But when I try to run the code i am getting an Error.

Below is the script which I am using in m ASP.NET page.
<script language="vbscript" type="text/vbscript">
          Sub Validate (val,arguments)
                If(val.Length=6) Then
                        arguments.IsValid=True
                Else
                        arguments.IsValid=False
                        
                End If
                
          End Sub
                
        
        </script>


Following is the code for the CustomValidtor.
<asp:customvalidator id="CustomValidator1" runat="server" xmlns:asp="#unknown">
            ErrorMessage="User name length should be minimum 6 characters" 
            onservervalidate="CustomValidator1_ServerValidate" 
            ClientValidationFunction="Validate" ControlToValidate="TextBox1"></asp:customvalidator>


Kindly assist.
Many Thanks in Advance
Posted
Comments
AspDotNetDev 16-Aug-10 14:42pm    
What error are you getting?
Mukund Kallapur 16-Aug-10 14:45pm    
The execution stops and asks whether to Break, Continue or Ignore pointing to this line "If(val.Length=6) Then"

1 solution

I wish I knew VBScript, but, I don't know it. However, I was able to run the Custom Validation properly using Javascript. Please try to modify the JavaScript code to VBScript, I believe it won't be hard for you :)

Here is the Code:

(Please note that, I also added a RequiredFieldValidator to validate the empty inputs)

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>Untitled Page</title>
    <script language="javascript" type="text/javascript">
          function Validate(sender, args)
          {
             if(args.Value.length == 6)
                args.IsValid = true;
             else
                args.IsValid = false;
          }
        </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:CustomValidator id="CustomValidator1" Display="Dynamic" runat="server"
            ErrorMessage="User name length should be minimum 6 characters"
            onservervalidate="CustomValidator1_ServerValidate"
            ClientValidationFunction="Validate" EnableClientScript="true" ControlToValidate="TextBox1"/><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic" ErrorMessage="User name required" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>
 
Share this answer
 
Comments
NeenaM 28-Mar-13 14:57pm    
i have used your above code but i am getting the following error

'ASP.validation_aspx' does not contain a definition for 'CustomValidator1_ServerValidate' and no extension method 'CustomValidator1_ServerValidate' accepting a first argument of type 'ASP.validation_aspx' could be found (are you missing a using directive or an assembly reference)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900