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:
CustomValidator control takes care of all the rest of the
Because validation can be performed on both the client (depending on the browser)
and the server, the
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
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 ...