AutoFocus plugin

Focus on the first invalid element when submit form


You have to use this plugin with SubmitButton plugin which automatically validate the form when pressing the Submit button. The following piece of code is the starting point to use the AutoFocus plugin:
<link rel="stylesheet" href="/vendors/formvalidation/dist/css/formValidation.min.css" />
<form id="demoForm" method="POST">...</form>
<script src=""></script>
<script src="/vendors/formvalidation/dist/js/FormValidation.min.js"></script>
<script src="/vendors/formvalidation/dist/js/plugins/AutoFocus.min.js"></script>
document.addEventListener('DOMContentLoaded', function(e) {
fields: {
plugins: {
autoFocus: new FormValidation.plugins.AutoFocus(),
submitButton: new FormValidation.plugins.SubmitButton(),
The sample code above assumes that the FormValidation files are placed inside the vendors directory. You might need to change the path depending on where you place them on the server.


onPrefocusFunctionInvoked before the first invalid element get focused
The onPrefocus function accepts one parameter:
onPrefocus: function(e) {
// e.field: The field element name
// e.firstElement: The first invalid element

Basic example

Try to press the Add product button to see the first invalid field will be focused. Then fill in the Product name, press the Add product button again. The plugin will bring focus to the Description field which now becomes the first invalid field.
AutoFocus plugin


  • The onPrefocus option allows to access the file name via the field property
  • Fixed an issue that the AutoFocus plugin doesn't work. From v1.5.0, it becomes an external plugin
  • Added onPrefocus option that is useful when you want to activate the tab containing the first invalid field
  • First release