ASP.NET: Syncing client and server-side validation rules

Are there any easy, smart ways to keep your client and server-side validation-rules synchronized?

On the client side we have JavaScript, maybe some kind of framework like jQuery or YUI.

On the server-side we have ASP.NET WebForms or ASP.NET MVC.

What is validated are things like:

  • Correct e-mail-addresses
  • Correct home-addresses and postal codes
  • Correct credit-card numbers

And so on.

Answers


You can try using Enterprise Library Validation with ASP.NET integration.


<asp:RegularExpressionValidator ...> (and the other asp.net validators) implement client side javascript and server side checking to the same rules.


write a large, common corpus of test data that embodies the validation rules, and unit test your validators against this common data.

When your rules change, you reflect this by updating the test data and testing until everything goes green again.


I have always used the built in validators. For example if you use a RegularExpressionValidator and supply a ValidationExpression it will validate on client side (if available) and server side using the same code.

You can write your own custom validators by deriving from BaseValidatior. Doing this allows you to create Server Valdiation by overriding EvaluteIsValid. You can then add client validation later if it is necessary.


This is not a real-world solution, but check out the Axial project on CodePlex. It is a project that converts C# to Javascript for the web, and has a control that lets you use the same code for server side validation and client side validation. It's not ready for production, but I'm curious to see if this is what you're looking for.


xVAL is quite a bit easier than the Enterprise Library Validation and handles model bound validation for both Client and Server.


Need Your Help

Naming convention for VB.NET private fields

vb.net convention

Is there an official convention for naming private fields in VB.NET? For example, if I have a property called 'Foo', I normally call the private field '_Foo'. This seems to be frowned upon in the