Choose form validation style carefully

In web applications, validation of user input can be done with two different tools: Server validation: Browser validation: Another important distinction between the two styles deals not with their behavior, but with their testability. Validation code on the server can always be tested economically using tools such as JUnit. During the entire life of an application, such tests can be quickly repeated using automated means, helping to identify possible errors caused by code changes. Although tools such as HttpUnit can test javascript behavior in web applications, such tests do not seem to be as simple to implement as JUnit tests on a Model Object.

Browser validation seems most useful for:

If using scripting, should validation be done after SUBMIT is pressed, or as each field is changed? If there are no dependencies between fields, there is not much difference between the two styles. However, if there are dependencies between fields, which is quite common, then the validate-after-SUBMIT style is likely to be simpler to implement.

Many prefer to treat server validation as mandatory and browser validation as optional, either as a performance optimization or as the preferred user experience. If both are implemented, then this will usually represent significant code repetition.

See Also :
Use a testing framework (JUnit)