views:

59

answers:

5

It seems to me that any method of input I provide for a user needs some scrutiny, but I have never seen someone implement a single set of validation that all inputs must pass.

Is there some basic validation you do on every single field a user can type in to? For the sake of simplicity let's just talk about text boxes.

Do you add any code to watch for control characters?

String length limits?

A: 

TextBox.MaxLength, TextBox.Text.Contains, aspValidators, etc.

I'm not sure if that was the kind of response you were looking for, were you assuming more of a best practices thing with a code example?

Woot4Moo
Just a best practice would be enough, but a code sample would be incredible.
JoshBaltzell
+1  A: 

Every input is different. It depends on your program. Sometimes fields are optional, sometimes they're required. Some fields are required if an optional field is filled in. It all depends on your application....

Jason
A: 

I would simply use regular expressions

e.g. (Examples in Perl regex)

/^[a-zA-Z]+$/   # non-blank, a to z only
/^[a-zA-Z]{3,10}$/    # a to z, 3 to 10 characters long
/^(\d-){2}-\d$/    # dd-mm-yy or similar format
ternaryOperator
I would recommend against this approach. Most people do regex when it is not necessary and there are far easier ways of doing things such as date checking. IE create a calendar widget. the length can be handled by simple text length calls to a string object. Just my .02 cents
Woot4Moo
Plus the asp.net RegularExpressionValidator will always validate a blank field as correct, even if the RegEx requires more than one character.
ristonj
I know there are usually alternative solutions, but regular expressions are usually a good method of implementing generic checks on input (note though that these are just simple examples - I would not advise actually using regular examples on any examples as simple as this).
ternaryOperator
+3  A: 

A CustomValidator can easily be programmed to validate every field on the page, so if there was an overarching validation requirement, that would be what I would use.

Unfortunately validation is never a "one size fits all" in my experience. Generally validation directly reflects your business logic, and every field is going to carry unique business requirements.

womp
+2  A: 

Is there some basic validation you do on every single field a user can type in to?

Generally, no. However, some specific apps may have that requirement for within that app, so for example all the text fields in the app are limited to 255 characters. Or an app may have certain kinds of inputs that share common validation requirements. For example, you might divide up your text inputs into just a few types: free-form, money, dates, etc.

What you can do in these cases is implement custom controls or user controls that wrap the validation logic for each kind of input with an input itself. Then just drop the right kind of control on your form, and the validation logic will follow it.

Joel Coehoorn