Solved

Custom Validation not working in Asp.net

Posted on 2010-11-23
3
370 Views
Last Modified: 2012-05-10
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
Comment
Question by:ptemo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 250 total points
ID: 34201003
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
 
LVL 3

Expert Comment

by:saifikram_md
ID: 34201699
Also postback in this case is correct as the validation is done on the serverside.
0
 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 250 total points
ID: 34205440
<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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question