Solved

ASP.NET Validators not preventing postback when validation fails

Posted on 2009-05-20
10
2,711 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
  • 4
  • 3
  • 3
10 Comments
 
LVL 10

Expert Comment

by:orbulat
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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
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

 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
It handles the validation client side before the postback?
0
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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