Jelle
Jelle

Reputation: 1060

How to change validation MVC to lost focus on field?

I'm using a modelclass with data anotations in MVC 4 to validate a form in my view. This works fine, however, the @Html.ValidationMessageFor(model => model.bijkomendeUitleg) shows my errors while typing...

For example I have a minimum length of 10 for a text field, when I start typing and I have less then 10 characters, the error shows up.

I want errors to show up when I lose focus on the textfield or when clicked on submit...

Is there any way to alter this behavior?

Upvotes: 1

Views: 4416

Answers (2)

Shariful_Islam
Shariful_Islam

Reputation: 361

Go to jquery.validate.js. There you will find:

$.extend($.validator, {......})

In it you find:

onkeyup: function(element, event) {
    if ( element.name in this.submitted || element == this.lastElement ) {
        this.element(element);
    }
},

Remove this code. I did not try this. But hope it will work for you.

Upvotes: 0

Gaurang s
Gaurang s

Reputation: 842

try this If you are using jquery 1.7.1 with unobtrusive validation, something like this should do the trick:

<script>
    $('input[data-val=true]').on('blur', function() {
    $(this).valid();
});

If using an older version of jquery, change to this:

<script>
    $('input[data-val=true]').blur(function() {
    $(this).valid();
});

Upvotes: 2

Related Questions