I using ASP.NET Validation Controls to validate fields on a form and I need to set the focus on a specific anchor within my page, but it keeps moving to the top of the page even though SetFocusOnError is set to True. I tried adding a CustomValidator with a ClientValidationFunction but it runs before validation hence moving back to the top of the page.
I also tried this:
Set focus to validation summary?
But the first link is no longer available and the second one just don’t work (That’s why answers should be self contained!).
Here are samples of my code:
<asp:ValidationSummary ID="ValidationSummary1" ClientIDMode="Static"
runat="server" HeaderText="[custom message in my native language]"
ValidationGroup="Valida" CssClass="msg_erro" />
<asp:RequiredFieldValidator ID="vldNome" runat="server"
ControlToValidate="txtNome" ValidationGroup="Valida" Display="Dynamic"
ErrorMessage="blah blah blah" SetFocusOnError="True" Text="*" />
//a bunch o others validatiors...
<asp:ImageButton runat="Server"
ImageUrl="~/img/anuncie/btn-cadastrar.png" ID="btnEnviar"
ValidationGroup="Valida" CssClass="btn-cadastrar" />
As I said, I need the focus to be on a specific anchor but setting the focus on the ValidationSummary or on any field would be nice as well because my fields start far from the top of the page.
Thanks.
UPDATE
Different behavior in different browsers:
- IE – Focus is correctly set on fields when clicking on submit (depending on IE version)
- FF – Clicking on submit for the first time moves to the top of the page, second time and on, it set the focus on the fields.
- Chrome – Never works.
Just switched to jQuery Validation plugin.