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
Solved

Custom Validation not working in Asp.net

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 …

839 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