JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Custom Validators

As discussed in the In Depth section of this chapter, to use a custom validator, you set the ClientValidationFunction property to the name of a script function, such as a JavaScript or VBScript function (VBScript is a Microsoft scripting language that supports a tiny subset of Visual Basic), both of which are supported in the Internet Explorer. This function will be passed two arguments, source, giving the source control to validate, and arguments, which holds the data to validate as arguments.Value. If you validate the data, you set arguments.IsValid to True, and to False otherwise. And as with other validation controls, you set the ControlToValidate property to set the control whose data you want to validate, and place the error message in the ErrorMessage property.

There's a custom validator in the ValidationControls example on the CD-ROM, next to the fifth text box from the top. This example lets the users apply for college, and the custom validator checks the amount of tuition they're willing to pay. In this case, I've added this script, written in VBScript, to the ValidationControls example's WebForm1.aspx file:

<script language=vbscript>

   Sub Validate(source, arguments)
      If (arguments.Value > 20000) Then
         arguments.IsValid = True
         arguments.IsValid = False
      End If
   End Sub


To see more on how to write scripts in Web pages, see references such as the Coriolis HTML Black Book.

In this script, I've created a procedure named Validate, where I'm just checking to make sure that the value the user has entered is greater than 20,000. To connect that to the custom validator in the ValidationControls example, you set the validator's ClientValidationFunction property to Validate. Because the value the user has entered is less than 20,000 in Figure 18.9, the custom validator is displaying its error message.

Click To expand
Figure 18.9: Using a custom validator.
Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor