Search code examples
asp.net-mvc-3jquery-uiremote-validation

ASP.NET MVC 3: model-level client-side or remote validation


I need to do some model-level validation in an MVC 3 edit page. (To be specific, I need to confirm that either Field A or Field B is filled in, but not both and not neither.)

I want to perform client-side validation as well as server-side validation, which means either using remote validation or implementing duplicate validation code. I'm OK with either.

I've read a number of posts on rolling your own server-side model-level validation, but none that deal with also implementing client side validation. (I don't know -- I'm sure someone out there can tell me -- whether model-level client-side validation is easy to set up with jQuery validation.)

I've also read about implementing your own remote validation from scratch, which I may have to do since the Remote attribute is property-level only.

I've read this question, which is identical to mine, but the only link that's really on-point doesn't seem to say what the answerer says it says.

So, my question: is there an easy, relatively low-effort way to implement server+client model-level validation, with or without a remote component? And is there a nice blog post or web page somewhere that explains this?


Solution

  • There is a similar question answered here which may be of some help? The answer given is for validating that at least one field is entered but the principles given in the answer may be what you are looking for and you should be able to change the answer to suit the validation you require. The solution also offers both server and client side validation options and I believe you can use the solution as both module or property level validation?

    Additionally, there is also the following article here detailing how to create your own custom validation similar to that provided in the answer I linked to.

    Hope this helps.