2
votes

I'm using ASP.NET MVC with unobtrusive validation. I need to add a new custom validation attribute with client-side validation, which is fine (I've already got some of those defined). The problem is that in the client side validation, I need to make an ajax call to check if the input is valid.

In MVC, you add client-side validators with addMethod:

jQuery.validator.addMethod("customValidation", function(){...})

But the function you define needs to return a boolean, and doing an ajax post is aysnc, which breaks that.

According to jQuery validator and a custom rule that uses AJAX there are 2 options - either use async:false in the ajax call or add a remote to the validate method. BUT, async:false is depcrated and I can't use remote because I'm using unobtrusive validation.

Has anyone got any better solutions to this?

1
Can you post the javascript validation method, or at least its logic ?jbl

1 Answers

0
votes

Data annotations contain a Remote attribute that plugs into jquery remote validation automatically.

https://msdn.microsoft.com/en-us/library/system.web.mvc.remoteattribute(v=vs.98).aspx