Solved

Custom Validation not working in Asp.net

Posted on 2010-11-23
3
340 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

11 Experts available now in Live!

Get 1:1 Help Now