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 provide the name of 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 done on the client or on the server, depending on the browser, the CustomValidator has attributes for specifying both a server-side and a client-side method for validation. The server-side method can be written in any .NET language, (we, of course will use Visual Basic 2005), while the client-side method must be written in a scripting language understood by the browser, such as VBScript or JavaScript.

Add one more row to your form (below the ZIP-code row), asking the user for an even number, as shown in Figure 9-23.

Even number row

Figure 9-23. Even number row

Once again, the middle column has a text box (txtEven). Drag a CustomValidator into the third column and set its attributes as shown in Table 9-9.

Table 9-9. CustomValidator properties and events

Property

Value

ID

cvEven

ControlToValidate

txtEven

ClientValidationFunction

ClientEventValidator

ValidateEmptyText

False

ErrorMesssage

Your number is rather odd

Text

*

ServerValidate ...

Get Programming Visual Basic 2005 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.