views:

332

answers:

9

How do you avoid duplication of validation on the server and client-side? Is there a web programming platform that generates one from the other, so I don't have to keep the two in sync?

+2  A: 

Most forsake client side scripting and just do server side nowadays. You can use ajax when sending the post, so that the response is a bit lighter weight than reloading the whole page.

Adam Luter
+2  A: 

Server-side validation is required simply because client-side validation can be disabled.

Client-side validation isn't required, but it makes your application more responsive during error handling, as you no longer need to do a form submit to the server and wait for the resulting page to come back.

A well-designed application uses both.

As a side note, Ajax can be used for certain kinds of validation, such as checking for duplicate usernames on a user registration form. However, basic validation, such as checking if a field contains only numbers, should be done without it.

R. Bemrose
+1  A: 

It's best to do validation on the server side in most cases. You have many different options to make this easier such as Ajax or using sticky forms in PHP. I personally tend to do validation on the server side because the user has the option to turn off JavaScript and they can't turn off validation on the server.....

Mark
+4  A: 

There are frameworks that can generate the client-side validation from serverside validation configuration.

Now I don't know what language or frameworks you are using, but maybe this article can be of help: http://www.emadibrahim.com/2008/09/08/client-server-side-validation-in-aspnet-mvc/

He's using Castle Validator Component together with JQuery and adds some glue to generate the clientside validation based on the serverside validation attributes. It's for asp.net mvc.

/Asger

asgerhallas
I'm looking for something not MS, and the example didn't really look like it would simplify things overall.
josh
Well the principle should be possible to transfer to other platforms as well... what language/framework/platform do you prefer?
asgerhallas
+3  A: 

As others mentioned, you should keep the duplication, as the client-side validation is to help the application react sooner to help the user, but, the real validation is on the server-side, as you should never trust anything passed in until it has been validated. You will probably do more extensive validation on the server-side, esp if there is a need to check against a data source, for example, is the username unique would be on the server-side, but is the username long enough, or an email address, could be done on the client-side, and server-side.

I tend to put in comments when there is duplication, especially if I am using a regular expression, to make certain that what changes in one is changed in another.

Good unit tests will help to ensure that these two will always remain in sync.

James Black
How does one write a unit test that will test both the server-side validator and the client-side validator? It seems like in the normal case, you'd end up with a duplicated set of unit tests (one for each language), and those unit tests could get out of sync.
Tony Arkles
You will have two tests, but the server-side will do more checks most likely than the client side, as the client is just looking to see if the data is roughly correct, but absolute correctedness is a job for the server.
James Black
A: 

What you can do is have the server-side validation logic being run by web services that your client-side validation can call via AJAX and also when you post back to your server.

Dan Appleyard
+1  A: 

This is not to answer your question, but you might want to modify your title so it does not confuse the reader, to something like "How to keep validation on the server and client-side synchronized?"

The idea of title is to either a) give the reader rough idea of what you are asking or b) make the reader realize he/she has to read your content to understand your question.

FlyinFish
+1  A: 

I think the best way is to use a component that offers both. Their code has been tested, and you don't need to maintain it. I have used Peter Blum's controls in the past with great success. Other than that I think you will have to maintain two code bases if you want to offer both. There is a tool called Script# which can help compile C# to Javascript (it does a translation, not a real compilation), but I am not sure how great it would work for this situation.

Bob
A: 

Asp.net Version 1 had validator controls to do what you are asking

http://msdn.microsoft.com/en-us/library/yb52a4x0.aspx

Chad Grant