Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

108

Saturday Afternoon

Note the boldfaced code. We have set the ControlToValidate property to the second password field. Once the user has entered the second password, a client-side validation will compare it against the first password field defined by the ControlToCompare property.

Additionally, we have set the Type property of the comparison to String to insure that when the Operator property Equal is applied that the comparison will work correctly. As already mentioned, you could use any number of operator enumerators to do the comparison as well as any of the property enumerators.

If we wanted to compare a value rather than two controls, you could simply set the ValueToCompare property to a specific string rather than use the ControlToCompare property.

RangeValidator

This control is useful to compare one control to values of two other controls or to a specific range. In Listing 11-4, we are simply checking to see if the user has entered a valid age range of equal or greater than 18 but less than or equal to 50 years old.

Listing 11-4 Using a RangeValidator Control

<%@ Page Language=”vb” %> <HTML>

<HEAD>

<SCRIPT LANGUAGE=”VB” RUNAT=”server”>

Sub Page_Load(Source As Object, E as EventArgs) If Page.IsPostBack Then

lblTitle.Text = “Submit was successful”

Else

lblTitle.Text = “Enter an age <18 or >50 and hit the Submit button”

End If End Sub </SCRIPT>

</HEAD>

<BODY>

<FORM ID=”WebForm1” METHOD=”post” RUNAT=”server” NAME=”WebForm1”> <P>

<ASP:LABEL ID=”lblTitle” RUNAT=”SERVER” /> </P>

<P>

Age

<ASP:TEXTBOX ID=”txtAge” RUNAT=”SERVER” HEIGHT=”24” WIDTH=”28”></ASP:TEXTBOX>

<ASP:RANGEVALIDATOR

ID=”RangeValidator1”

ERRORMESSAGE=”You must be older than 18 and less than 50 to

register”

RUNAT=”SERVER”

CONTROLTOVALIDATE=”txtAge”

BACKCOLOR=”#FFFF80”

MINIMUMVALUE=”18”

TYPE=”Integer”

MAXIMUMVALUE=”50”>

</ASP:RANGEVALIDATOR>

Session 11—Validating User Input

109

</P>

<P>

<ASP:BUTTON ID=”btnSubmit” RUNAT=”SERVER” TEXT=”Submit”></ASP:BUTTON> </P>

</FORM>

</BODY>

</HTML>

Most of the properties on this control we have used in the previous examples. The new property, in boldface, of MinimumValue establishes the minimum range of the compare. MaximumValue establishes the upper range of the comparison. If you wanted to utilize the values of other controls, you could use the properties MaximumControl and MinimumControl, setting the values equal to the id’s of the controls you want to compare against.

Several factors for this control should be noted. If the user leaves a control blank, the control passes the range validation. To force the user to enter a value, add a RequiredField validation control as well. If both MaximumControl and MaximumValue are specified, then the MaximumControl is used. If both MinimumControl and MinimumValue are specified, then MinimumControl will be used to perform the range validation.

CustomValidator

Although the above controls should cover 90 percent of your validation needs, there will be scenarios where you will want to take a value the user enters, apply an algorithm, compare it to a database value, or run it against a Web service to determine if the information is valid. In these cases, you can utilize the CustomValidator control. This control enables you to define both clientand server-side validation routines to compare a control value against. These functions must return Boolean true or false to process the appropriate error message for the control.

For the current example, you are going to compare a subscription code provided by the user against a fixed value. Listing 11-5 illustrates how you can utilize a custom server-side function and a custom client-side function to perform validation.

In Listing 11-5, we have created a server-side function ValidateSubscriptionServer that simply accepts the control value as a string and sets objArgs.IsValid equal to true or false depending upon the result. In this case, you simply check to see if the user control you are validating has a text value equal to abc123. However, you could have also performed a database query or any other type of routine to do the comparison.

Next, we have included a client-side validation routine. The routine is a Javascript 1.0- compliant function called ValidateSubscriptionClient that runs on the client side. Since this function does not have the RunAt = Server attribute, it can have the same name as our server-side function, but will be processed as soon as a user moves their mouse from the Subscription Code field.

Listing 11-5 Using a CustomValidator Control

<%@ Page Language=”vb” %> <HTML>

<HEAD>

<SCRIPT LANGUAGE=”VB” RUNAT=”server”>

Continued

110

Saturday Afternoon

Listing 11-5

Continued

Sub Page_Load(Source As Object, E as EventArgs)

End Sub

Public Sub ValidateSubscriptionServer(objsource As Object, objArgs As ServerValidateEventArgs)

If strComp(objArgs.Value, “abc123”, CompareMethod.Text) = 0 Then objArgs.isValid=True

lblTitle.Text = “Subscription value accepted on client and server!” Else

objArgs.isValid =False

lblTitle.Text = “Subscription value rejected on server!” End If

End Sub </SCRIPT>

<SCRIPT LANGUAGE=”javascript”>

function ValidateSubscriptionClient(objSource,objArgs)

{

if(objArgs.Value==”abc123”)

{

objArgs.IsValid= true;

}

else

{

objArgs.IsValid=false;

}

return;

}

</SCRIPT>

</HEAD>

<BODY>

<FORM ID=”WebForm1” NAME=”WebForm1” METHOD=”post” RUNAT=”server”> <P>

<ASP:LABEL id=”lblTitle” RUNAT=”SERVER”></ASP:LABEL> </P>

<P>

Subscription Code

<ASP:TEXTBOX id=”txtSubscription” RUNAT=”SERVER”></ASP:TEXTBOX>

<ASP:CUSTOMVALIDATOR id=”CustomValidator1” RUNAT=”SERVER” ONSERVERVALIDATE=”ValidateSubscriptionServer” CLIENTVALIDATIONFUNCTION=”ValidateSubscriptionClient” BACKCOLOR=”#FFFF80” CONTROLTOVALIDATE=”txtSubscription” ERRORMESSAGE=”This Subscription Code is Not Valid”>

</ASP:CUSTOMVALIDATOR>

</P>

<P>

<ASP:BUTTON id=”btnSubmit” RUNAT=”SERVER” TEXT=”Submit”></ASP:BUTTON> </P>

</FORM>

</BODY>

</HTML>