开发者

Datannotations + asp.net mvc 3 + client side validation (does not kick in until a form submit happens)

开发者 https://www.devze.com 2023-02-07 08:11 出处:网络
I am using asp.net mvc 3, jquery validate, unobstructed javascript and data annotations. I noticed that say your in textbox A that is a required and click out of it no validation wills how. It\'s lik

I am using asp.net mvc 3, jquery validate, unobstructed javascript and data annotations.

I noticed that say your in textbox A that is a required and click out of it no validation wills how. It's like the javascript has not kicked in. If they click the submi开发者_如何转开发t button then the validation pops up.

Then if they go back to textbox A and start typing in it the validation goes away. So the client side validation is working.

I am wonder is there away to get the client side validation to kick in right away?


Here's what's going on and why.

  1. Textbox starts as empty. You tab in and out and no validation happens (unless you hit submit)
  2. You enter some values. You tab about and now validation fires (but your textbox has a value so no error)
  3. You go back to the textbox and clear the value. You tab out and now you get the validation message.

The reason for this behavior is that we don't want to start popping up errors all over the form just because the user can't decide which field they want to start filling out first.


That's how ASP.NET MVC 3 unobtrusive client side validation has been implemented. If you want to change this behavior you could always write your custom validation rules and options with the jquery.validate plugin:

$('#myForm').validate({
    rules: {
        name: {
            email: true
        }
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消