• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Custom Validation not working in Asp.net

Hello all,

Newbie here trying to figure out how to do custom validation.  I'm trying to do a code that checks the length.  It seems to show up the error message, but the page is still allowed to post.  I'm not sure what is going on.  Please see my code below.

<%@ Page Language="C#" %>


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

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "works";
               
    }

    void valComments_ServerValidate(Object source, ServerValidateEventArgs args)
    {


        if (args.Value.Length <= 10 && ins.Text == "1")
            args.IsValid = false;
        else
            args.IsValid = true;

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


<head id="Head1" runat="server">

    <script type="text/javascript">

        function valComments_ClientValidate(source, args)
        {


            if (args.Value.Length <= 10 && ins.Text == "1")
                args.IsValid = false;
            else
                args.IsValid = true;

        }
   
    </script>


    <title></title>
</head>
<body>
    <p>
        ins</p>
    <form id="form1" runat="server">
    <p>
        <asp:TextBox ID="ins" runat="server"></asp:TextBox>
    </p>
    <p>
        97110</p>
    <p>
        <asp:TextBox ID="ther" runat="server"></asp:TextBox>

    <asp:CustomValidator
        id="valComments"
        ControlToValidate="ther"
        Text="(Comments must be less than 10 characters)"
        OnServerValidate="valComments_ServerValidate"
        Display="Dynamic"
        ClientValidationFunction="valComments_ClientValidate"
        Runat="server" />
       
       
    </p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        message</p>
    <p>
        &nbsp;</p>
    <div>
   
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   
    </div>
    </form>
    </body>
</html>



Thanks
0
ptemo
Asked:
ptemo
2 Solutions
 
Miguel OzSoftware EngineerCommented:
use client validatiion only to avoid postback.
Change js as follows:
        function valComments_ClientValidate(source, args)
        {


            if (args.Value.Length <= 10 && $get(ins).value == "1")
                args.IsValid = false;
            else
                args.IsValid = true;

        }
0
 
saifikram_mdCommented:
Also postback in this case is correct as the validation is done on the serverside.
0
 
Alpesh PatelAssistant ConsultantCommented:
<html xmlns="http://www.w3.org/1999/xhtml">


<head id="Head1" runat="server">

    <script type="text/javascript">

        function valComments_ClientValidate(value)
        {


            if value.Length <= 10)
                return false;
            else
                return  = true;
        }
   
    </script>


    <title></title>
</head>
<body>
    <p>
        ins</p>
    <form id="form1" runat="server">
    <p>
        <asp:TextBox ID="ins" runat="server"></asp:TextBox>
    </p>
    <p>
        97110</p>
    <p>
        <asp:TextBox ID="ther" runat="server"></asp:TextBox>

    <asp:CustomValidator
        id="valComments"
        ControlToValidate="ther"
        Text="(Comments must be less than 10 characters)"
        Display="Dynamic"
        ClientValidationFunction="valComments_ClientValidate(this.value)"
        Runat="server" />
       
       
    </p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        message</p>
    <p>
        &nbsp;</p>
    <div>
   
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   
    </div>
    </form>
    </body>
</html>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now