There are six validation controls included with ASP.NET as well as a summary control. Data validation with the ASP.NET validation controls is always processed on the Web server and can also optionally be processed on the user's computer.
by Shannon Horn
Aug 7, 2003
Page 6 of 8
To perform custom validation beyond that already provided by the other ASP.NET validation controls, the CustomValidator control processes a custom script that must return true or false to indicate success or failure of the validation process.
ASP.NET processes all validation controls on the server and, optionally, on the user's computer. You must write a custom function member for the CustomValidator to process on the Web server. You may also write a script for the CustomValidator to process on the user's computer.
In the HTML snippet below, a CustomValidator validates the value entered into a TextBox control to determine if the value is a prime number.
ErrorMessage="The number entered is not a prime
Here I've specified the txtPrimeNumberTextBox control as the control to validate using the ControlToValidate attribute of the CustomValidator control. I want the CustomValidator control to process two scripts: ClientValidation and ServerValidate, which I specify using the ClientValidationFunction attribute and the OnServerValidate attribute, respectively.
When the CustomValidator calls the ClientValidationFunction, it passes a reference to the calling object as well as the attributes associated with the calling object. The Value property of the txtPrimeNumber TextBox control as well as the IsValid property are accessed through the arguments parameter that is passed.
The OnServerValidate attribute identifies the function member to process on the Web server for data validation. This function member can carry out a completely different process than the ClientValidationFunction. However, in most cases, the function member on the Web server and the script on the user's computer are identical in functionality. Here I've used C# to write the ServerValidate function member used in this example.