Creating Your Own Validation
There may be times when you can't find one of the built-in validation
controls that handles your specific data. For example, which control could
verify that a number the user has entered is an even number (if that was a
requirement of your page)? There isn't such a control, but ASP.NET provides
the CustomValidator control, which allows you to specify procedures to be
run in order to validate the data in the associated input control.
For this example, your goal is to ensure that users enter either CA, NV, or
AZ into the State text box on the page. You could use a DropDownList
control for this, or you could use a RegularExpressionValidator control
(setting the expression to be CA|NV|AZ). However, for the sake of this
example, suppose you want to take advantage of the CustomValidator control.
The CustomValidator control requires you to supply code for the control's
ServerValidate event梚t calls this code as it attempts to validate the data
on the server. No matter what browser has loaded the page, this code will
run before the entire page can be validated. If the browser supports
client-side script, it is nice to provide the script to run from within the
browser, on the client side, to provide the same sort of experience you get
when working with the other validation controls.
Therefore, you'll need to write the event procedure, in the page's
code-behind file, to provide server-side validation. In addition, in the
page itself, you'll need to insert the client-side script that will be sent
down to the browser. (In addition, you'll need to set the control's
ClientValidationFunction property to indicate the name of the client-side
function.) Both procedures receive two parameters: The first parameter
contains the object that raised the event (the CustomValidator control
itself), and the second parameter is a ServerValidateEventArgs object
containing information about the event. You can use the Value property of
the second parameter to retrieve the value the user entered, and you'll set
the IsValid property of the argument to indicate whether the value is valid.
To add validation for the State text box, follow these steps:
Add a CustomValidator control adjacent to the State text box.
Set the properties for the control as shown in Table 8.6.
Table 8.6. Set the CustomValidator Control's Properties to Match These
Items Property Value
ID cvalState
ClientValidationFunction ValidState
ControlToValidate txtState
Display Dynamic
ErrorMessage Enter CA, NV or AZ
Double-click the CustomValidator control and modify the ServerValidate
procedure so that it looks like this:
Private Sub cvalState_ServerValidate( _
ByVal source As System.Object, _
ByVal args As _
System.Web.UI.WebControls.ServerValidateEventArgs) _
Handles cvalState.ServerValidate
Select Case args.Value
Case "CA", "NV", "AZ"
args.IsValid = True
Case Else
args.IsValid = False
End Select
End Sub
TIP
The ServerValidate event handler uses the Value property of its second
parameter to determine the value the user has entered and the IsValid
property to indicate whether the value is valid.
Close the code-behind file and then press Ctrl+PgDn (or use the View, HTML
Source menu item) to view the HTML source for the page.
Immediately below the <BODY> element, near the top of the page, add the
following script code, which provides the client-side validation:
<SCRIPT language="VBScript">
Sub ValidState(source, arguments)
Select Case arguments.value
Case "CA", "NV", "AZ"
arguments.IsValid = True
Case Else
arguments.IsValid = False
End Select
End Sub
</SCRIPT>
Now that you've added both the server-side and the client-side validation
procedures, browse to the page and verify that entering an invalid state
does trigger the validation code.