• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3352
  • Last Modified:

ASP.NET Validators not preventing postback when validation fails

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
paulott
Asked:
paulott
  • 4
  • 3
  • 3
1 Solution
 
orbulatCommented:
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
 
paulottAuthor Commented:
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
 
orbulatCommented:
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
Shaun KlineLead Software EngineerCommented:
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
 
paulottAuthor Commented:
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
 
Shaun KlineLead Software EngineerCommented:
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
 
paulottAuthor Commented:
It handles the validation client side before the postback?
0
 
Shaun KlineLead Software EngineerCommented:
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
 
orbulatCommented:
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
 
paulottAuthor Commented:
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

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now