Sunday, 10 February 2008

International Standard Book Numbers (ISBNs), like other important numbers, such as credit card numbers, lend themselves to self-validation. The last digit of an ISBN is a check digit. A value found by applying a mathematical formula to other digits of the ISBN can be compared to this check digit. If the values relate to each other in a certain way, the ISBN is taken to be of a valid format.

To use an ASP.NET custom validator to apply the validation logic, proceed as follows. (The code validates ten-digit ISBN numbers.)

First, drag a CustomValidator control to the design surface of Visual Studio. Set its ControlToValidate to the ID of the control that is to be validated (eg. a TextBox). Set the ErrorMessage and Text properties as required. Double-click the CustomValidator control and add the following code to the ServerValidate event handler (this is example starter code: you will need to check its suitability against your particular requirements):

To perform client-side validation too, you can write a similar function in JavaScript and attach it to the CustomValidator’s ClientValidationFunction property. The function should have the following signature: