asp.net mvc 3 - Why unobtrusive MVC validations are not working in IE7 and IE8? -
i have application in asp.net mvc 3 using razor. have setup data annotations on text boxes along jquery validation , unobtrusive validation.
the thing is, fields have custom validation logic using plain old vanilla javascript following functions
.
function validation() { var signup = signupvalidation(); var book = bookingvalidation(); if (signup && book) { return true; } else { showerrormessage(); return false; } } function showerrormessage() { $('span#errormessage').html('these fields required'); }
where
signupvalidation()
,bookingvalidation()
functions returns eithertrue
orfalse
on basis of other validation logic.
this code submit button.
@using (html.beginform(mvc.booking.actions.atwork(model: null), formmethod.post, new {@onsubmit = "return validation()" })) { @html.partial("_bookingview") }
this approach working in browsers except ie-7/8.
i faced same issue lately .. , worked out following solution: instead of giving additional form validation (apart unobtrusive mvc 3 validation) separate/second submit handler in form "onsubmit" event, should "inject" additional validation function in main unobtrusive validation process of mvc3.. let take care of rest.
create custom validation adaptor somewhere in common javascript code/file:
(function ($) { if($.validator && $.validator.unobtrusive) { $.validator.unobtrusive.adapters.addbool("additionalformvalidation"); } } (jquery));
in view file, have form, add code create new jquery validator method custom validator adaptor defined in common file above:
(function ($) { if ($.validator) { $.validator.addmethod("additionalformvalidation", function (value, element) { return validation(); }); } } (jquery));
here - "additionalformvalidation" validator method name same custom validation adaptor. - "validation" name of javascript function takes care of additional validation , returns boolean result validation successs or failure.
in form, remove "onsubmit" handler had supplied, add invisible dummy text field form , apply custom unobtrusive validation adaptor/rule created, given below:
@using (html.beginform(mvc.booking.actions.atwork(model: null), formmethod.post)) { @html.partial(mvc.booking.views._bookforatwork) <input type="text" style="visibility:hidden; width: 1px; height: 1px;" name="hiddenvalidation" id="hiddenvalidation" data-val="true" data-val-additionalformvalidation /> }
this solution worked charm me. me appears cleaner solution injects additional validation in same unobtrusive validation flow of mvc3 rather creating second validation flow. inline future improvement creating custom data annotation (validations) custom client side validation work.
Comments
Post a Comment