Custom Validation
Sometimes 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 the rest.
Because validation can be done on the client or on the server, depending on the browser, the CustomValidator has attributes for specifying a server-side and a client-side method for validation. You can write the server-side method in any .NET language, such as C# or VB.NET, but you must write the client-side method in a scripting language the browser can understand, such as VBScript or JavaScript.
To demonstrate, add a copy of RegexValidator.aspx to the C11_Validation website and call it CustomValidator.aspx. In this example, you’ll add a new row to ensure that the user enters an even number before the bug is submitted.
Tip
In the real world, antispam measures such as CAPTCHA and Akismet randomly generate a series of characters for the user to type rather than just a number divisible by 2. You could also use this technique to perform a checksum on a credit card or ISBN or otherwise perform complex data checking. However, we’re demonstrating the CustomValidator here, and not those systems.
Add a new row to the table immediately above the Submit Bug button row. You’ll need to add a TextBox control (txtEvenNumber) for the user ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access