O'Reilly logo

Learning ASP.NET 2.0 with AJAX by Dan Hurwitz, Jesse Liberty, Brian MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Custom Validation

There are times when the validation of your data is so specific to your application that you will need to write your own validation method. The CustomValidator is designed to provide all the infrastructure support you need. You point to your validation method and have it return a Boolean value: true or false. The CustomValidator control takes care of all the rest of the work.

Because validation can be performed on both the client (depending on the browser) and the server, the CustomValidator has attributes for specifying a server-side and a client-side method for validation. The server-side method can be written in any .NET language, such as C# or VB.NET, but the client-side method must be written in a scripting language understood by the browser, such as VBScript or JavaScript.

The code functionality is duplicated on the server for two reasons. First, as mentioned at the beginning of this chapter, it prevents a malicious user from bypassing the client-side validation, and second, it makes the page compatible with older browsers which may not support client-side validation.

To get you started, once again copy the RegularExpressionValidator web site to a new site named CustomValidator. In this example, you want to ensure that the user enters an even number.

This time, you'll report an error if the number is not evenly divisible by 2. You can imagine, however, that you could use this technique to perform a checksum on a credit card or ISBN number or otherwise perform ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required