Basic validation for every field
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 p开发者_运维百科ass.
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 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.
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.
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....
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?
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
精彩评论