Solved

Custom Validation not working in Asp.net

Posted on 2010-11-23
3
348 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
3 Comments
 
LVL 35

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 53
Error in page 3 46
Not showing page correctly 3 31
Adding  DYMO Labelprinter to c# client application 4 11
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

864 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now