Listing 54 Validator Testaspx which uses the Range Validator Regular ExpressionValidator and Custom Validator control

<%@ Import Namespace="System.Web.Security " %>

<%@ Import Namespace="System.Web.Security " %>

<script language="C#" runat=server>

void Validate_Click(Object sender, EventArgs E) {

Msg.Text="Page Valid";

void CustomServerVal (object source, ServerValidateEventArgs args) {

Msg.Text="ServerValidation called and TRUE returned."; args.IsValid=true;

Msg.Text="ServerValidation called and FALSE returned."; args.IsValid=false;

catch {

Msg.Text="ServerValidation called and FALSE returned."; args.IsValid=false;

</script> <body> <form runat=server> <center> <h3>

<font face="Verdana" color=blue>Validator Test Page</font> </h3> <table> <tr> <td>

<input id="Range"


<ASP:RangeValidator ID="ValRange" ControlToValidate="Range" Display="Static" Type="Integer" MinimumValue="1" MaximumValue="12" ErrorMessage="Out of Range" runat=server />

Regular Expression Validation (nnn-nn-nnnn): </td> <td>

<input id="RegEx" type="text"

<ASP:RegularExpressionValidator ID="ValRegEx" ControlToValidate="RegEx" runat="SERVER" ErrorMessage=

"Enter a valid U.S. SSN (nnn-nn-nnnn)." ValidationExpression= "[0-9]{3}-[0-9]{2}-[0-9]{4}" />

Custom Validation (It wants you to enter "Hello" WITHOUT THE QUOTES): </td> <td>

<input type="text" id="txtCustom" runat=server size=11 /> </td> <td>

<ASP:CustomValidator ID="ValCustom"


ControlToValidate="txtCustom" OnServerValidate="CustomServerVal"

Display="Static" >

Enter "Hello". Case-Sensltlve. </ASP:CustomValldator> </td> </tr> <tr>

<td colspan=3 align="center"> <asp:button text="Valldate" OnClick="Validate_Click" runat=server>

<asp:Label id="Msg" ForeColor="red" Font-Name="Verdana" Font-Slze="10" runat=server />

</td> </tr> </table> </center> </form> </body> </html>

Figure 5-7 shows ValidatorTest.aspx when displayed in a browser.

iMIm'.-bfalhud tdibtortnt MUwfe^i>|iH -l-,,,v.HI 1,

9a Em tM FW«*H I«Js nm>

■ — - J jfctfl ¿lima-. jjFiwt«

J> ^ J


Rqgylar E^msscn Validation (iu-_~-iu-_-iuim)

Validator Test Page

Rqgylar E^msscn Validation (iu-_~-iu-_-iuim)

Qwfsm VjudilKHi fft wmn3 yviuio tufei ■Etdo- WITHOUT THE QUOTES)

Valid alt

Figure 5-7 : ValidatorTest.aspx, which uses the RangeValidator, RegularExpressionValidator, and CustomValidator controls The RangeValidator control has several attributes that are unique to this type of validator. In Listing 5-4, the code that declares RangeValidator is as follows:

<ASP:RangeValidator ID="ValRange"






ErrorMessage="Out of Range"

runat=server />

Three of the attributes used here are already familiar (ControlToValidate, Type, and RunAt), one has been shown previously but not explained (Display, to be described in the next section), and a couple are new (MinimumValue and MaximumValue). Type is the type of comparison that should be done. For example, consider whether 1234 is greater than 13. If these are string values, 1234 is alphabetically smaller, but if these are numeric values, 1234 is numerically greater. The values allowed for Type in RangeValidator are the same as are allowed for a CompareValidator control's Type attribute.

MinimumValue and MaximumValue are compared to the value of ControlToValidate, using the type conversion specified by the Type attribute. In this example, we're looking for an integer from 1 through 12.

RegularExpressionValidator is useful because of its flexibility. The code used to specify RegularExpressionValidator in Listing 5-4 is shown here:

<ASP:RegularExpressionValidator ID="ValRegEx"



ErrorMessage="Enter a valid U.S. SSN (nnn-nn-nnnn)."

ValidationExpression="[0-9]{3}-[0-9]{2}-[0-9]{4}" /> The unique attribute in the code to create this validator is ValidationExpression. The value of this attribute is a regular expression pattern to match against the value of the control specified in ControlToValidate.

If you're unfamiliar with regular expressions in general, refer to the following "Regular Expressions" sidebar.

0 0

Post a comment