9.4. Summary

User input is an important aspect of most interactive web sites. It comes from different sources in your web site: the contact form you created in this chapter, the query string, and other sources. To stop users from entering invalid or even dangerous content into your system, it's important to validate all input before you work with it.

The validation controls that ASP.NET supports make it easy to validate all data coming from form controls, such as TextBox, DropDownList, Calendar, and so on. The RequiredFieldValidator helps to ensure that a user has filled in a required field. The RangeValidator, CompareValidator, and RegularExpressionValidator are used to check the contents of the data against settings you define at design time. The CustomValidator allows you to write validation code that is not covered by the built-in validation controls, giving you maximum flexibility. The ValidationSummary control is used to give feedback about the errors that your users made while entering data. It can display these errors as a simple bulleted list in the page, or as a JavaScript alert window.

The biggest benefit of the validation controls is that they work at the client and at the server, enabling you to create responsive forms where a user gets immediate feedback about any errors they may have made, without the need for a full postback. At the same time, the data is validated at the server, ensuring that data coming from clients that don't support JavaScript is valid as well. ...

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.