remio
remio

Reputation: 1280

How to reuse c# code for data validation in javascript?

We have some c# code that implements data validation rules for several entity types.
In some situations, the data has to be validated also on the client side.
I'm looking for a way to implement only once those validation rules that would be used either on server side (C#) or on client side (javascript).

For now, the only think I have thought about was to make the most of the fact that C# and javascript have a similar syntax by storing the method body as source code and either build it at runtime for C# or return it to the client for Javascript.

I was wondering if other solutions existed.

Upvotes: 0

Views: 825

Answers (1)

AlexC
AlexC

Reputation: 10756

One option is to implement MVC3 which has a feature called “Unobtrusive Client Validation”. You set validation attributes on your model and this validation is implemented on both the client, through JQuery and the server, via C#.

This is a few useful links: Unobtrusive Client Validation in ASP.NET MVC 3

If you can't fully use MVC, because of the change between your current architecture and the MVC approach MVC is of course open source so you can get under the hood to look at their implementation.

Upvotes: 1

Related Questions