开发者

ASP.NET: Custom Validation not working even when required field validator is used

Here is a code for my web form control

<asp:TextBox runat="server" ID="txtUsername"></asp:TextBox&开发者_JAVA技巧gt;
<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>


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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜