ASP.NET Validation Controls. Friend or Foe?

This comes up on the message boards quite frequently.  The question normally starts out as, “How do I _________ with the _____ validation control?”  Or, if they are really looking for a solution rather than an implementation, they’ll ask how to do it with the validation controls instead of one specific control.

Then somewhere in the course of the thread someone will say something like, “it’s situations like that that have caused me to not use the validation controls…”  Most recently, it was an MVP who seems to be everywhere.  But, an answer like this makes me question every answer he’s likely to give on the forum.

Are there some situations that are more difficult than others?  Yes.  Should we not use them at all because of those situations?  Well, let’s do a cost benefit analysis:

Using validation controls gives us:

  • Client side AND server side validation by writing minimum code
  • Allows us to assign a validation to a specific control
  • Allows us to group validations and controls so that only specific validations fire for specific regions of the screen.
  • Gives us multiple, but consistent, ways of reporting the validation error back to the user.
  • I don’t have to learn javascript if I don’t want to.
  • Provides a custom validation control for the odd ball case which conforms to how the other validation controls work so that all we have to do is write the validation but displaying the error message, firing the validation, etc is left to the framework.
  • I might have to have multiple validations and multiple controls on the screen that do essentially the same thing because any specific control or validation can only belong to one validation group.

So far it looks like using validation controls would be much faster. 

Not using validation controls:

  • I’m stuck writing validations by hand myself or using some other framework (one I probably wrote myself and therefore isn’t tested as well)
  • I have to write code to display the error on the screen.
  • I have to make sure I’m consistent in how I display the error from one screen to another.  This is particularly problematic when you are working with multiple developers.
  • All of the developers in my organization have to know javascript AND
  • I can code validations any way I want.
  • I might get greater code re-use.

Sorry, I’m not seeing a big advantage here.  In fact, I’ve been coding ASP.NET applications and teaching others to use ASP.NET since beta 1 of version 1.0.  I’ve yet to see a situation where the validation controls wouldn’t work or where anyone could argue that they were not the appropriate solution.  This is especially true now that we have the ability to group the controls and validations.

So, here’s the challenge.

Anyone that has a situation they think will not work using the existing set of validation controls is welcome to post a comment with their specific situation.  Maybe someone can change my mind. Tags: ,

Technorati Tags: ,

Related Post

Comments are closed.