Performing Custom Validation Adding Validation

280 Displays the error messages in paragraph form. EnableClientScript Specifies whether to enable a client-side script if supported by the browser. The default is True . ForeColor Defines the color in which the summary is displayed. The default is Red . Valid values for this attribute are given by the Color structure defined in the System.Drawing namespace. See Chapt er 4 for information about the Color structure. HeaderText Displays a message above the summarized items. The default is an empty string. ShowMessageBox Indicates whether to show the summary in a message box using a client-side script. If this attribute is set to True and the browser supports JavaScript, the summary is shown in a message box see Figur e 6- 11 . If set to False , no message box is shown. The default is False . Figure 6-11. Showing the validation summary in a message box ShowSummary Indicates whether to show the summary on the web page. If this attribute is set to True , the summary is shown on the web page. If set to False , the summary is not shown on the web page. The default is True . Note that the ShowMessageBox and ShowSummary attributes are independent of each other. If both are set to True , the summary is shown in both ways. These attributes all correspond to like-named properties of the ValidationSummary class and can be read and modified in code.

6.4.4 Performing Custom Validation

For situations in which the standard validation controls arent sufficient, ASP.NET provides the CustomValidator control. This control allows the developer to specify custom functions—on both the client and the server—that validate the contents of the associated control. The client-side validation function must be a JavaScript or VBScript code block that is part of the web page. The server-side validation function is a method defined in the web page class. Ex am ple 6- 8 shows code using a CustomValidator control. The code generates a page with a single text box in which the user is expected to type a mathematical expression. The validation that occurs 281 on the value simply ensures that there are an equal number of left and right parentheses. Figur e 6- 12 shows the display before validation occurs. Figur e 6- 13 shows the display when validation fails. Figure 6-12. The display from Ex a m ple 6 - 8 before validation occurs Figure 6-13. The display from Ex a m ple 6 - 8 when validation fails Example 6-8. Using the CustomValidator control Page Explicit=True Strict=True script language=VB runat=server Private Sub cstExpression_ServerValidate _ ByVal sender As Object, _ ByVal e As ServerValidateEventArgs _ e.IsValid = _ CountSubStringe.Value, = CountSubStringe.Value, End Sub Private Function CountSubString _ ByVal strSearchThis As String, _ ByVal strSearchFor As String _ As Integer Dim count As Integer = 0 Dim pos As Integer = strSearchThis.IndexOfstrSearchFor Do While pos -1 count += 1 pos = strSearchThis.IndexOfstrSearchFor, _ pos + strSearchFor.Length Loop Return count 282 End Function script script language=JavaScript function countSubStringsearchThis, searchFor { var count = 0; var pos = searchThis.indexOfsearchFor; while pos = -1 { count++; pos = searchThis.indexOfsearchFor, pos + searchFor.length; } return count; } function cstExpression_ClientValidatesender, e { e.IsValid = countSubStringe.Value, == countSubStringe.Value, } script html head titleCustomValidator Testtitle head body form action=CustomValidatorTest.aspx method=post runat=server table tr td align=rightEnter an expression:td tdasp:TextBox id=txtExpression runat=server td tr tr td colspan=2 asp:CustomValidator id=cstExpression ControlToValidate=txtExpression OnServerValidate=cstExpression_ServerValidate ClientValidateFunction=cstExpression_ClientValidate ErrorMessage=The number of left parentheses in the expression must be the same as the number of right parentheses. runat=server td tr tr td colspan=2 align=center asp:Button id=btnSubmit text=Submit runat=server td tr table form body html 283 The code in Ex am ple 6- 8 is straightforward. First, a server-side method called cstExpression_ServerValidate is added to the page to handle server-side validation. This method is actually an event handler with a signature thats appropriate for the CustomValidator controls ServerValidate event. Setting the OnServerValidate attribute of the asp:CustomValidator tag wires the handler method to the event. Second, a client-side function, cstExpression_ClientValidate, is added to the page to handle client-side validation. This function is called by the client-side validation code when it is time for the txtExpression field to be validated. Setting the ClientValidateFunction attribute of the asp:CustomValidator tag wires up this function.

6.5 Using Directives to Modify Web Page Compilation