Avoid double clicking submit with validator display set to dynamic - asp.net

I have some ASP.NET CustomValidators with Display="Dynamic". The layout of the page requires that I use them like this without a ValidationSummary.
<asp:CustomValidator ID="vldFirstThree" ControlToValidate="txtFirstThree" runat="server" OnServerValidate="ValidateTextBox" ValidateEmptyText="true" ErrorMessage="Please enter the first three characters of the owner name.<br />" CssClass="error" Display="Dynamic" Enabled="true"/>
If the user leaves the field blank and clicks submit, the error text is displayed. If the user fixes the problem and then clicks submit again, the validation error text clears, but a second click is required to submit the page. I've tried writing some server-side code and some JavaScript to cause the validated TextBox to blur() before submitting, but I'm still running into the need to click twice: once to clear the message and again to submit.
Is there a way to clear the error text and perform the submit with one click?

The answer is the second one here: RequiredFieldValidator have to click twice.
I had to set EnableClientScript="false".

Related

Issue with Required Field validation in a asp.net application having two pages workflow

I have a asp.net application with two pages. The first page is having a next button and second page is having a back and submit button. The second page contains text box control with required field validtor.
As per current design, required field validator on 2nd page should get fired on clicking on submit button.
The issue I am having is :- User Fills the first page and then go to second page. Now, user wants to comeback to first page and then go to second page, the required field validator on text box is getting fired and showing up the error message. I dont want this. This should be like - text box should be blank and no error message should be displayed. Error messages should be displayed on clicking on the Submit button on the second page.
I tried couple of options :- 1. putting InitialValue on required field etc.. but no luck.
Could you pls help me out??
The following Markup should solve your problem :
<asp:TextBox ID="tbData" runat="server" ValidationGroup="Submit"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Button" ValidationGroup="Submit" />
<asp:Button ID="btnBack" runat="server" Text="Button" />
<asp:RequiredFieldValidator ID="reqFieldVal1" runat="server" ErrorMessage="Field must be filled in."
ControlToValidate="tbData" ValidationGroup="Submit"></asp:RequiredFieldValidator>
These fields use the ValidationGroup property to set which fields will cause the validation of which validator controls.
You will notice that there is no ValidationGroup property on the btnBack button as this should not cause validation.
For more information on this see the MSDN Article for Specifying Validation Groups

RequiredFieldValidator requires user to click twice

I have a simple web form with a textbox and a RequiredFieldValidator wired up to it. When the RequiredFieldValidator error is triggered the user has to click the submit twice to post the form. The first click clears the error, the second actually fires off the button click event. Is this expected behavior?
<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server"></asp:RequiredFieldValidator>
<asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px"></asp:TextBox>
<asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />
I tried adding CausesValidation to the textbox per a suggestion found from a Google search and it doesn't help.
EDIT It seems that it doesn't always have to be a double click to fire off the event. As long as text is entered into the textbox and then the focus is taken away from the textbox, the RequiredFieldValidator error message goes away and the form requires only a single click.
I had the same issue with a CompareValidator and found the problem went away when I changed the Display property from Dynamic to Static. Hope that helps
This happens because the code that clears out the error message runs when the textbox loses focus. So what happens is:
You enter text in the field
You click on the button, which causes the onblur event to happen on the textbox, firing the code to check the field's value again and removing the error message
Now there are no errors in validation, so clicking the button again submits the form.
When you press the tab key first (or basically do anything that takes the focus off the textbox), then that onblur script runs and clears out the error so that when you click the submit button it is ready to go.

2 user controls on registered on one aspx page not validating validation properly

I have 2 user controls on registered on one aspx page.
UserControl1 us having one text box with require field and one submit button.
UserControl2 is also having one text box with requirefiled and save button.
Expected o/p is-
When I am clicking on any button out of 2(submit or save). Then only related text boxof that user control should be validate.
But the error is
Both text boxes are validate.
Please help me .
Set the ValidationGroup properties to limit which fields get validated when the buttons are pressed.
So for example, if these were contained within the first user control:
<asp:requiredfieldvalidator id="NameValidator"
controltovalidate="NameTextBox"
validationgroup="UserControlOne"
errormessage="required"
runat="Server" />
<asp:button id="Submit"
text="Submit"
causesvalidation="true"
validationgroup="UserControlOne"
runat="Server" />
Clicking the "Submit" button would only cause the validators that have UserControlOne specified as the ValidationGroup to validate.
Edit: When you call Page.Validate() you are validating every group on the page. Call the overloaded Page.Validate(validationGroup) to validate a specific one. e.g. Page.Validate("UserControlOne")

Asp.net regularexpressionvalidator fires even when CausesValidation="false"

I have a regularexpression validator which validates a valid email. I have two buttons on my form. Submit and undo.
On undo, we are reverting the page state to default
submit has it validationgroup set while undo doesnot have any validationgroup and CausesValidation="false".
Now when i navigate to page and enter invalid emailaddress,i directly click undo. the validator fires and stops my page from posting.however if i press tab and navigate to other control and then click undo,the validator shows error message but posts back and furthur proessing is done.
This is very strange and i want the page to postback without any error message when i click undo.how to achieve it.
<tec:ThemedImageButton runat="server" ID="imgbtnSave" OnClick="imgbtnSave_Click"
ValidationGroup="CustomerGroup"/>
<tec:ThemedImageButton runat="server" ID="imgbtnCancel" CausesValidation="false"
OnClick="imgbtnCancel_Click" />
These are normal image buttons with added themes.CustomerGroup is the validation group for my textbox and regularexpressionvalidator
Are you possibly clicking the undo whilst your focus is still within the email textbox? If so, perhaps the issue is that the initial blur event of the textbox is firing which in turn is calling the email validator and preventing the click / submission.
The first thing to try is to make sure you can enter an invalid email address, move the focus onto a completely different area of the page and then press undo to determine if this is the cause.
Double check your settings.
You might want to check the code behind to make sure you are not overwriting the control settings there. For example, in code, you might have the CausesValidation set to "True" or something like that.
Until you post more information you could do the following in your Undo button:
<asp:Button ID="btnUndo" runat="server" Text="Undo"
OnClientClick="Page_ValidationActive = false;"
OnClick="btnUndo_Click" />
I don't recommend this as it might solve your problem but really doesn't explain what is wired up incorrectly but the Page_ValidationActive = false; will disable all client validation on your page.

Can't change an asp page because of a required validator

On one of my asp.net pages I have a few textboxes and a required validator attached to them. Everything is ok with validation, but when I want to change page to another required validator doesn't allow it because I don't fill in a textbox :/
How to allowed change page without fill a validate textbox? I must create a condition in "exit page" (how is it named ?) event disabled required validation?
Please help ;)
If you're using some control to move to the next page, set it's CausesValidation property to False.
For example, if you're clicking a button that moves you to the next page, it would be like:
<asp:LinkButton id="myButton" runat="server" CausesValidation="False" .... />

Resources