11<environment include =" Development" >
2- <script src =" ~/lib/jquery-validation/dist/jquery.validate.js" ></script >
3- <script src =" ~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js" ></script >
2+ <script src =" ~/lib/jquery-validation/dist/jquery.validate.js" ></script >
3+ <script src =" ~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js" ></script >
44</environment >
55<environment exclude =" Development" >
6- <script src =" https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"
7- asp-fallback-src =" ~/lib/jquery-validation/dist/jquery.validate.min.js"
8- asp-fallback-test =" window.jQuery && window.jQuery.validator"
9- crossorigin =" anonymous"
10- integrity =" sha256-F6h55Qw6sweK+t7SiOJX+2bpSAa3b/fnlrVCJvmEj1A=" >
11- </script >
12- <script src =" https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"
13- asp-fallback-src =" ~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
14- asp-fallback-test =" window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"
15- crossorigin =" anonymous"
16- integrity =" sha256-9GycpJnliUjJDVDqP0UEu/bsm9U+3dnQUH8+3W10vkY=" >
17- </script >
6+ <script src =" https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"
7+ asp-fallback-src =" ~/lib/jquery-validation/dist/jquery.validate.min.js"
8+ asp-fallback-test =" window.jQuery && window.jQuery.validator"
9+ crossorigin =" anonymous"
10+ integrity =" sha256-F6h55Qw6sweK+t7SiOJX+2bpSAa3b/fnlrVCJvmEj1A=" >
11+ </script >
12+ <script src =" https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"
13+ asp-fallback-src =" ~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
14+ asp-fallback-test =" window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"
15+ crossorigin =" anonymous"
16+ integrity =" sha256-9GycpJnliUjJDVDqP0UEu/bsm9U+3dnQUH8+3W10vkY=" >
17+ </script >
1818</environment >
1919<script type =" text/javascript" >
20- // Add/remove class 'input-validation-error' to the div containing the control with error
21- $ (function () {
22- $ (' form' ).each (function () {
23- if ($ (this ).data (" validator" )) {
24- var valSettings = $ (this ).data (" validator" ).settings
25- valSettings .highlight = wrap ($ .validator .defaults .highlight , highlightDecorator)
26- valSettings .unhighlight = wrap ($ .validator .defaults .unhighlight , unhighlightDecorator)
27- }
20+ // Add/remove class 'input-validation-error' to the div containing the control with error
21+
22+ let errorElements = [];
23+ let submitAttempted = false ;
24+ document .querySelector (' form' ).onsubmit = () => { submitAttempted = true ; }
25+
26+ $ (function () {
27+ $ (' form' ).each (function () {
28+ if ($ (this ).data (" validator" )) {
29+ var valSettings = $ (this ).data (" validator" ).settings
30+ valSettings .highlight = wrap ($ .validator .defaults .highlight , highlightDecorator)
31+ valSettings .unhighlight = wrap ($ .validator .defaults .unhighlight , unhighlightDecorator)
32+ }
33+ });
2834 });
29- });
3035
31- function wrap (functionToWrap , beforeFunction ) {
32- return function () {
33- var args = Array .prototype .slice .call (arguments );
34- beforeFunction .apply (this , args);
35- return functionToWrap .apply (this , args);
36+ function wrap (functionToWrap , beforeFunction ) {
37+ return function () {
38+ var args = Array .prototype .slice .call (arguments );
39+ beforeFunction .apply (this , args);
40+
41+ if (errorElements .length && submitAttempted) {
42+ errorElements[0 ][' obj' ].focus ();
43+ submitAttempted = false ;
44+ }
45+
46+ return functionToWrap .apply (this , args);
47+ };
3648 };
37- };
3849
39- function highlightDecorator (element , errorClass , validClass ) {
40- $ (element).closest (" div" ).addClass (errorClass).removeClass (validClass);
41- }
42- function unhighlightDecorator (element , errorClass , validClass ) {
43- $ (element).closest (" div" ).addClass (validClass).removeClass (errorClass);
44- }
50+ function highlightDecorator (element , errorClass , validClass ) {
51+ $ (element).closest (" div" ).addClass (errorClass).removeClass (validClass);
52+ let itemExists = errorElements .some (obj => obj[' id' ] == element .id );
53+ if (! itemExists) {
54+ errorElements .push ({ ' id' : element .id , ' obj' : element });
55+ }
56+ }
57+ function unhighlightDecorator (element , errorClass , validClass ) {
58+ $ (element).closest (" div" ).addClass (validClass).removeClass (errorClass);
59+ errorElements = errorElements .filter ((elm ) => elm .id != element .id );
60+ }
4561
4662 </script >
0 commit comments