Understanding the Error: An invalid form control is not focusable in HTML Forms
markdown
When working with HTML5 forms, you may encounter a runtime validation error such as:
An invalid form control with name='metaKeyword' is not focusable.
An invalid form control with name='metadesc' is not focusable.
This error appears when the browser tries to focus a required form element that is hidden or not currently focusable. This is part of the browser’s automatic HTML5 constraint validation.
Browsers validate required fields when a form is submitted. If a required field is empty or invalid, the browser attempts to:
Stop the form submission
Focus the first invalid input
Display the validation message
However, when the invalid input is hidden, disabled, or located in a non-visible tab or modal, the browser cannot focus it. This results in the error:
An invalid form control is not focusable.
Examples include:
display: none
visibility: hidden
Hidden Bootstrap modals
Closed accordions or tabs
For example, a field inside an inactive Bootstrap tab:
<div class="tab-pane fade" id="seo" aria-hidden="true">
<input required type="text" name="metaKeyword">
</div>
Because the tab is not active, the browser cannot focus the input.
requiredIf a script adds required to an input that is not visible, it becomes impossible to focus.
If JavaScript attempts to focus an element that is not visible, this error may appear.
required from Hidden FieldsIf a field is not always required:
$('input[required]').each(function () {
if (!$(this).is(':visible')) {
$(this).removeAttr('required');
}
});
Activate the correct tab before submission:
$('form').on('submit', function(e) {
let invalid = this.querySelector(':invalid');
if (invalid && !$(invalid).is(':visible')) {
const tabId = $(invalid).closest('.tab-pane').attr('id');
$('[data-bs-target="#' + tabId + '"]').tab('show');
}
});
If a field must always be filled, ensure it is placed in a visible portion of the UI.
Add required only when the field is visible:
$("#seoTab").on('shown.bs.tab', function () {
$('#metaKeyword, #metadesc').attr('required', true);
});
$("#seoTab").on('hidden.bs.tab', function () {
$('#metaKeyword, #metadesc').removeAttr('required');
});
| Issue | Explanation |
|---|---|
| Invalid form control | Field violates HTML5 validation rules |
| Not focusable | Field cannot receive focus due to hidden state |
| Result | Browser produces error and blocks submission |
To prevent this error, ensure that required inputs are visible and focusable at the moment the form is submitted, or apply dynamic validation logic based on UI visibility.