Windows form control validating validated
nput validation is something that every program needs, and every programmer dreads.Most developers craft hard-coded program logic to verify that a phone number consists of 7 or 10 digits; that an IP address consists of 4 octets; that a country name matches 1 of 193 choices, etc. NET 2.0 provides some support for streamlining input validation, it barely scratches the surface of automating and simplifying the input validation process.
Furthermore, it's unnatural to conceptualize the steps of validation as of the data to be validated.the effort expended to get this far would be multiplied five-fold).So, for example, you'll find that the engine doesn't handle validation interaction between multiple fields; instead, I opted to focus on single-field validation for this introductory article.Initial Solution: The Error Provider Component To begin, you need to be aware of the handy Microsoft component called Error Provider (see the MSDN article "Error Provider Component Overview").You'll find that using the validation engine discussed here, you can instrument a wide range of validation criteria with very few lines of code.The Problem You want your program to validate form input, provide feedback to the user, and perhaps take other actions to handle the condition. Limitations This article presents a useful solution, though not a solution for every case.
Instead, it follows the 80/20 rule: The validation engine will be useful in about 80 percent of the use cases you might need to address.
Also following the 80/20 rule, to cover the remaining use cases would require 80 percent of the effort (i.e.
For example, if you were describing the input, you might say, "It should be between five and eight characters; it should be numeric and non-negative; it should always end in the digit '5,' etc." The concept of considering the data qualities rather than the validation process leads to the validation engine discussed in this article.
A modular, data-driven validation engine approach provides a more natural interface, more flexibility, and is easier to maintain.
Visual Studio lets you specify property values at design time for a variety of components; using that approach for defining validation attributes is a natural extension, and makes incorporating robust validation simpler for developers.
The flexibility of storing validation settings in a configuration file makes validation easy to change, allowing developers and administrators to modifiy or customize validation properties without recompiling and redeployingand without needing the source code.