I am using MVC 3 unobtrusive validation. When validation fires it sets the focus on the first invalid control. How do i stop it doing this, i am more used to web forms and the equivalent would be to set the SetFocusOnError=false
on the validation control.
Asked
Active
Viewed 2,593 times
2

Ben Robinson
- 21,601
- 5
- 62
- 79
-
Hello @Ben, check this link: http://stackoverflow.com/questions/6475175/how-to-move-focus-to-first-error-field-with-mvc-client-validation – Felipe Oriani Nov 23 '11 at 13:36
-
I have seen that already, that's the exact opposite of what i want, i want the validation NOT to set the focus on the invalid field, at the moment it does. – Ben Robinson Nov 23 '11 at 13:41
-
@BenRobinson have you solved this problem? I have similar requirements. – Lav Jun 18 '12 at 23:18
2 Answers
8
Try setting a global option:
$.validator.setDefaults({
focusInvalid: false
});

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
1It doesn't work, the focus still goes to the first invalid field, although it does look like the right setting now i have tracked down the relevant documentation. The puzzling thing is that if i hack the script to make the default for focusInvalid to be false it works. – Ben Robinson Nov 25 '11 at 09:22
3
I'm not able to test at the moment, but I remember it being similar to this:
var settings = $.data($('form')[0], 'validator').settings;
settings.focusInvalid= false;

twaggs
- 3,591
- 1
- 14
- 8
-
It doesn't seem to work but i don't think it is a million miles off because when i had a look through the non minified version of the jq validator script i found a section that set some defaults the was setting `focusInvalid: true`. Changing it to false prevents the behavior but i would prefer to set it programatically in my view because i don't necessarily want it false for the entire app. – Ben Robinson Nov 23 '11 at 14:39
-
1Try this: var settings = $.data($('form')[0], 'validator').settings; settings.focusInvalid= false; – twaggs Nov 23 '11 at 17:49
-
I just wrote a long rambling answer and realised you'd beat me to it. The reason this works is [explained here](http://stackoverflow.com/questions/5487139/how-can-i-customize-the-unobtrusive-validation-in-asp-net-mvc-3-to-match-my-styl) – Liam Jun 03 '15 at 11:10
-
1You can also access the data more elegantly using `var validator = $('#formselector).data('validator');` – Liam Jun 03 '15 at 11:11