RequiredFieldValidator requires user to click twice
Asked Answered
K

2

7

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.

Knawel answered 16/8, 2011 at 23:47 Comment(3)
If you press the Tab key after entering the text in the textbox, do you still have to click the submit button twice?Precondemn
@Precondemn - Yep. Looks like when the focus is taken away from the text box the error goes away. Then the button only requires one click.Knawel
Take a look here #11122225Prophase
P
1

This happens because the code that clears out the error message runs when the textbox loses focus. So what happens is:

  1. You enter text in the field
  2. 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
  3. 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.

Precondemn answered 17/8, 2011 at 14:53 Comment(0)
T
6

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

Trapezoid answered 21/6, 2012 at 8:57 Comment(0)
P
1

This happens because the code that clears out the error message runs when the textbox loses focus. So what happens is:

  1. You enter text in the field
  2. 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
  3. 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.

Precondemn answered 17/8, 2011 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.