Asp.net auto focus of required field validator is

2019-08-19 06:54发布

问题:

I am working with asp.net web forms application. In a page there are required field validators. If I miss any required field and click in the submit button required field validator should show error and automatically focus on the missed input field. It is working in the all major browser like chrome, firefox,IE. But auto focusing is not working in safari 9.0.1.

Am I missing anything or any change required ? Thanks in advance.

Here is my sample code:

<asp:TextBox runat="server" ID="txtFirstName" CssClass="form-control" placeholder="First Name" />
<span class="help-block text-left">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtFirstName" ValidationGroup="send" runat="server" ErrorMessage="*" CssClass="left" Font-Size="14px" ForeColor="#da7070" Display="Dynamic"></asp:RequiredFieldValidator></span>

回答1:

Set the SetFocusOnError to true. Default behaviour is false.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" SetFocusOnError="true" ControlToValidate="txtFirstName" ValidationGroup="send" runat="server" ErrorMessage="*" CssClass="left" Font-Size="14px" ForeColor="#da7070" Display="Dynamic"></asp:RequiredFieldValidator>