Form Field Validation via Regular Expressions

I have a problem. Of course, or I wouldn't be posting here. Ok, now that Mr. Obvious is out of the way ...

I'm looking for some ideas. Take a look at the code below. I'm looking for the most efficient way to validate form field input on the server-side. I'll provide what I have. What I'm looking for are improvements.

Now, I'll obviously want to do more than write "error" to the screen with the validated input. What I *really* want ideas on is a more efficient way to validate the fields. In this small example doing them as one-offs isn't bad. On a form that has 20-30 fields it becomes cumbersome.

You should try testing for the more obvious errors such as, is the input null, are there between 10 and 14 characters, are there any non apha characters, are there two words separated by a space etc.
If these fail then you should go on to the more extensive and time consuming testing. You might want to do some statistical analysis on the most common type of errors that your set of everyday users produce. Then make the tests for the most common errors the first and fastest.

If you just want to make the validation calls more tidy you can put the names of the input fields you have in the form in an dictionary named "inputs" and an place the type of validation needed in another dictionary named "valid"

Then do a "For Each" on the Request.Form object making sure that the form element exists in your dictionary. If it isn't then somebody added an extra field to your form!
Then just call the appropriate validation function depending on the type stored for that input. Loop until you have done all fields on the form.

You could also use arrays instead of dictionaries but dictionaries have efficient methods for testing existance and lookup.

Sorry for not getting back sooner. I've been working through the dictionary object.

Interesting solution. I'm working through a similar modification to what you suggested.

1. I'm creating a single dictionary object to hold the fields and their values.

2. I'm calling the validate function for each item: ugly, but it's working.

3. I've modified the validation function to accept things like:

Min_Length
Max_Length
Can_Be_NULL
HTML_Enabled
Required

This allows me to check, within my function, the various things that are important. If certain criteria are met then I create a second dictionary object containing the fields with errors.

My thought is to pass the errors dictionary object back to the form in a session variable and redirect using "server.transfer" to maintain the form collection. This will allow me to re-populate the form with the current data and use the session to mark bad input.