Browse Source

#15: Focus to the first invalid element

nghuuphuoc 12 years ago
parent
commit
0a2b506c23

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ## v0.2.2
 
 * Fixed #34: Avoid from calling form submit recursively
+* Improved #15: Focus to the first invalid element
 * Improved #31: remote validator: Allow to set additional data to remote URL
 
 ##v0.2.1 (2013-11-08)

+ 1 - 1
demo/submitHandler.html

@@ -64,7 +64,7 @@
                 // validator is the BootstrapValidator instance
                 // form is the jQuery object present the current form
                 form.find('.alert').html('Thanks for signing up. Now you can sign in as ' + validator.getFieldElement('username').val()).show();
-                form.submit();
+                //form.submit();
             },
             fields: {
                 username: {

+ 6 - 0
dist/js/bootstrapValidator.js

@@ -93,6 +93,12 @@
                                 that._setLiveValidating();
                             }
 
+                            // Focus to the first invalid field
+                            for (var i in that.invalidFields) {
+                                that.getFieldElement(i).focus();
+                                break;
+                            }
+
                             e.preventDefault();
                         } else {
                             if (!that.submitHandlerCalled && that.options.submitHandler && 'function' == typeof that.options.submitHandler) {

File diff suppressed because it is too large
+ 1 - 1
dist/js/bootstrapValidator.min.js


+ 6 - 0
src/js/bootstrapValidator.js

@@ -93,6 +93,12 @@
                                 that._setLiveValidating();
                             }
 
+                            // Focus to the first invalid field
+                            for (var i in that.invalidFields) {
+                                that.getFieldElement(i).focus();
+                                break;
+                            }
+
                             e.preventDefault();
                         } else {
                             if (!that.submitHandlerCalled && that.options.submitHandler && 'function' == typeof that.options.submitHandler) {