5

Here is a code for my web form control

<asp:TextBox runat="server" ID="txtUsername"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtUsername" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:CustomValidator OnServerValidate="checkUsername" ID="CustomValidator1" runat="server" ControlToValidate="txtUsername" EnableClientScript="true" ClientValidationFunction="checkUsername" ErrorMessage="CustomValidator"></asp:CustomValidator>

Client side validation

<script type="text/javascript">
        function checkUsername(source,args){
        alert("test");
           /* alert(args.Value);
            args.IsValid=false;
            */
        }
    </script>

Server side validation

protected void checkUsername(object sender, System.Web.UI.WebControls.ServerValidateEventArgs e) {
            String str=e.Value;
            if(str.Length>6)
            e.IsValid = false;
        }

But for some reason this Costom Validation is not firing . Any clues ?

EDIT: I am coding in asp for first time , Server validation is in code-behind class

Here is a code asp.net page , Maybe some small mistake I am making ?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="lab1.Registration" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Button ID="Button1" runat="server" Text="Recreate table" 
            onclick="Button1_Click" />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>

    </div>
    <script type="text/javascript">
        function checkUsername(source,args){
        args.IsValid=(args.Value.length<=6);
           /* alert(source.Value);
            args.IsValid=false;
            alert(document.getElementById("txtUsername").nodeValue);*/
        }
    </script>
    <asp:Table ID="Table1" runat="server" Height="106px" Width="469px">

        <asp:TableRow>
            <asp:TableCell>
                <asp:Label runat="server" Text="Username"></asp:Label>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox runat="server" ID="txtUsername"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtUsername" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                <asp:CustomValidator OnServerValidate="checkUsername" ID="CustomValidator1" runat="server" ControlToValidate="txtUsername" EnableClientScript="true" ClientValidationFunction="checkUsername" ErrorMessage="CustomValidator"></asp:CustomValidator>

            </asp:TableCell>
        </asp:TableRow>

    </asp:Table>

    </form>
</body>
</html>
3
  • what isn't working? i pasted your code into a brand new aspx page and the client side validation is firing the alert box Commented Jan 15, 2010 at 13:08
  • I just tried it bare Username control removing other controls .. Required field validation is working as it was before but custom is not Commented Jan 15, 2010 at 13:16
  • 1
    When i use your code, if i put nothing in the box and tab off (or click button) it shows requiredfieldvalidator. if i enter characters length more than 6 then i get the customvalidator error message. What are you experiencing that is different? Commented Jan 15, 2010 at 13:31

3 Answers 3

1

Ok, try changing your two methods like this.

<script type="text/javascript"> 
    function checkUsername(source,args){    
        args.IsValid=(args.Value.length<=6);
    }
</script>

and

protected void checkUsername(object sender, System.Web.UI.WebControls.ServerValidateEventArgs e)
{
    e.IsValid = (e.Value.Length <= 6);
}

The only real difference is it works out the true and the false IsValid, rather than only setting it to false.

If this isn't the problem then please elaborate a bit more on exactly what isn't working.

EDIT: Just to add to this... If it's just a length of input that you're interested then why not use a RegularExpressionValidator?

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that but Costom validation continues to fail. I can use reg expressions but I want to validate many controls using Costom validator it has to succeed
you need to be more clear on what "fail" is. what isn't happening that you expect to happen? or what happens that you don't expect to happen?
0

This might happen when adding controls to a page that already has controls being validated. In these scenarios often the other controls are validated first and validation never occurs on the new controls.

This can be resolved by adding a ValidationGroup property that is specific to the controls and button that have been added.

Comments

0

All tha above methods failed for me because my controls were within an update panel (asp.2.0).

Finally page.isValid() saved my day.

Also check : http://forums.asp.net/t/1216367.aspx/1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.