开发者

With ASP.Net MVC3 unobtrusive validation, is it possible to activate real time validation before the form is submitted?

When using MVC3 unobtrusive validation, the user initially fills in the form without any validation occurring. After the form is submitted and validation errors occur, validation then occurs in real-time as the user type开发者_运维问答s and moves between fields. Is it possible to have this real-time validation behaviour switched on before the form is submitted?


You could call $form.valid() to validate the whole form on page load but then they get a validation summary at the top (assuming you've enabled it) along with validation messages next to each input.

How about validating each field as it is changed? You could attach to the change event like so: $fields.change(function (event) { $(event.target).valid() }); which would run valid() on the field after it is changed. You can of course substitute another event for change to match more closely the function you need.

I don't know of an official way to mimic the post-submit-attempt validation exactly...


Make sure both ClientValidationEnabled and UnobtrusiveJavaScriptEnable are set to true in your web.config.

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />


Try this:

var isValid = $('#formId').validate().form();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜