Solved

ASP.NET Validators not preventing postback when validation fails

Posted on 2009-05-20
10
3,033 Views
Last Modified: 2013-12-17
I have a form and some asp:textboxes and some validators. If the information isn't valid, I don't want it to postback. Seems pretty simple, but can't seem to get it to work like this. I've tried removing the <xhtmlConformance mode> tag, I've played around with various combinations using OnClientClick, EnableClientScript, and CausesValidation, and trying to return false to the ImageButton if it fails, etc. Nothing seems to prevent the postback when the input is invalid. Help!

This would be easy in classic asp, but I'm trying to learn this new .NET stuffs...

Two examples of code I've tried are below. We're using .NET 2.0 C#.

Oh, also I'll toss an assist to anyone able to tell my why my code isn't doing the popup for the ValidationSummary even though I have ShowMessageBox="true" and ShowSummary="false".
function ValidateForm() {
    if (Page_IsValid) {
        document.getElementById("btn_Submit").disabled = true;
        return true;
    } else {
        return false;
    }
}
 
...
 
<form runat="server" id="main_form" onsubmit="Form_Submit">
     Email address
     <asp:TextBox ID="email1" name="email1" runat="server" size="15" maxlength="55" CausesValidation="true" />&nbsp;&nbsp;&nbsp;&nbsp;
      Reenter your email address 
     <asp:TextBox ID="email2" name="email2" runat="server" size="15" maxlength="55" CausesValidation="true" /><br />
     <asp:ImageButton ImageUrl="btn_go.gif" ID="btn_Submit" name="btn_Submit" CausesValidation="true" OnClientClick="javascript:return ValidateForm();" runat="server" Text="Submit" />
     <asp:CompareValidator id="CompareEmails" runat="server" ErrorMessage="Email addresses do not match." Display="dynamic" ControlToValidate="email1" ControlToCompare="email2" Type="String" EnableClientScript="true" />
     <asp:RequiredFieldValidator ID="RequiredEmail1" runat="server" ErrorMessage="Please enter the same email address in both boxes." ControlToValidate="email1" />
     <br /><asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please fix the following:" EnableClientScript="true" ShowMessageBox="true" ShowSummary="false" ForeColor="red" BackColor="yellow" />
</form>
 
___________another way I've tried it below________________________
 
 
<form runat="server" id="main_form">
      Email address
     <asp:TextBox ID="email1" name="email1" runat="server" size="15" maxlength="55"  />&nbsp;&nbsp;&nbsp;&nbsp;
      Reenter your email address 
     <asp:TextBox ID="email2" name="email2" runat="server" size="15" maxlength="55"  /><br />
      <asp:ImageButton ImageUrl="btn_go.gif" ID="btn_Submit" name="btn_Submit"  OnClick="Form_Submit" runat="server" Text="Submit" />
     <asp:CompareValidator id="CompareEmails" runat="server" ErrorMessage="Email addresses do not match." Display="dynamic" ControlToValidate="email1" ControlToCompare="email2" Type="String" />
     <asp:RequiredFieldValidator ID="RequiredEmail1" runat="server" ErrorMessage="Please enter the same email address in both boxes." ControlToValidate="email1" />
      <br /><asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please fix the following:"  ShowMessageBox="true" ShowSummary="false" ForeColor="red" BackColor="yellow" />
</form>

Open in new window

0
Comment
Question by:paulott
[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
  • 4
  • 3
  • 3
10 Comments
 
LVL 10

Expert Comment

by:orbulat
ID: 24437479
perhaps try this?

document.getElementById('btn_Submit').disabled = 'disabled'
or
document.getElementById('btn_Submit').style.visibility = 'hidden'

maybe u can use an alternative
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_21210052.html
0
 
LVL 1

Author Comment

by:paulott
ID: 24437498
Hey orbulat, thanks for the comment.

I'm not worried about the button being pushed twice. What I don't want is a postback to the server when the first level of validation is supposed to be handled in the client.  For example, if they did not enter an email1 address, I don't want it to post to the server, then come back with the error message that is in the RequiredFieldValidator. I want it to just display the error in the RequiredFieldValidator  via the client and javascript. Does that make sense?
0
 
LVL 10

Expert Comment

by:orbulat
ID: 24438434
i get what u mean, i think i came across sth. like this before
eventually i make the imagebutton always return false and run a js function
the js function contains validator's work, only do the post when it's valid.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24440324
If you have the Required Field validator on the email1 field, you should also have a Required Field validator on the email2 field to guarantee something has been entered into both fields. The Compare validator does not validate that the fields you reference have a value, and can cause problems when you are expecting a value where one does not exist.
0
 
LVL 1

Author Comment

by:paulott
ID: 24442362
orbulat, yeah, I know I could do this in classic ASP with javascript, which is what makes it frustrating when these ASP.NET controls don't seem able to do it.

Shaun, if I use a Required for email1 and Compare email1 to email2, you're saying it will allow if email1 has "123" and email2 is blank? You mean the Compare doesn't compare empty values to filled values?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24442835
I just verified that the Compare validator will handle checking "123" against blank. (Old habits, I guess.) I was able to take your code, modify it (removing the javascript function) and verify that this will work in a straight ASPX page, at least in IE 6 and FireFox.
     Email address
     <asp:TextBox ID="email1" name="email1" runat="server" size="15" maxlength="55" />    
      Reenter your email address 
     <asp:TextBox ID="email2" name="email2" runat="server" size="15" maxlength="55" /><br />
     <asp:ImageButton ImageUrl="~/accept.gif" ID="btn_Submit" name="btn_Submit" CausesValidation="true" runat="server" Text="Submit" />
     <asp:CompareValidator id="CompareEmails" runat="server" ErrorMessage="Email addresses do not match." Display="dynamic" ControlToValidate="email1" ControlToCompare="email2" Type="String" EnableClientScript="true" />
     <asp:RequiredFieldValidator ID="RequiredEmail1" runat="server" ErrorMessage="Please enter the same email address in both boxes." ControlToValidate="email1" EnableClientScript="true" />
     <br /><asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please fix the following:" EnableClientScript="true" ShowMessageBox="true" ShowSummary="false" ForeColor="red" BackColor="yellow" />
     <asp:Label ID="Results" runat="server" />

Open in new window

0
 
LVL 1

Author Comment

by:paulott
ID: 24442859
It handles the validation client side before the postback?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24442938
It does handle the validation client-side. If the email1 field has not been modified and the field loses focus, the Required Field validator does not fire, but when the ImageButton is clicked, the validator does fire. (I'm guessing that this behavior was by design to allow the user to tab through fields and not get the pesky error messages unless they actually did something to the field.)
0
 
LVL 10

Expert Comment

by:orbulat
ID: 24447159
here is my example on requirevalidator and comparevalidator
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test Validator</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Txt1: <asp:TextBox runat="server" ID="txtBox1"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="reqVal1" ControlToValidate="txtBox1" ErrorMessage="please enter txtbox1"></asp:RequiredFieldValidator>
        <br />
        Txt2: <asp:TextBox runat="server" ID="txtBox2"></asp:TextBox>
        <asp:CompareValidator runat="server" ID="compVal1" ControlToValidate="txtBox1" ControlToCompare="txtBox2" ErrorMessage="txt1 =/= txt2!" ></asp:CompareValidator>
        <br />                
        <asp:ImageButton runat="server" ID="imgbut1" ImageUrl="button1.gif" CausesValidation="true" AlternateText="click" OnClick="imgbut1_Click" />
        
    </div>
    </form>
</body>
</html>
 
codebehind -->
 
  protected void imgbut1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("validation is ok");
    }

Open in new window

0
 
LVL 1

Accepted Solution

by:
paulott earned 0 total points
ID: 24456654
I had to change the OnClientClick in my first example to "if (!ValidateForm()) return false;" and added a ValidationGroup to them. That seemed to help prevent it from posting back to the server for validation and handling it completely in the client. Thanks for trying guys.
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

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 …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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