Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net

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>
like image 810
Xinus Avatar asked Nov 14 '22 13:11

Xinus


1 Answers

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?

like image 115
Robin Day Avatar answered Dec 18 '22 19:12

Robin Day