Work With Web Controls

TYPE THIS:

<SCRIPT LANGUAGE=nC#" RUNAT="Server"> void Date_Selected(object s, EventArgs e) {

inputMessage.Text = calendarGoal.SelectedDate.ToShortDateString();

</SCRIPT></HEAD><BODY> <FONT FACE ="Verdana">

<H3>Welcome to mylifetimegoals.com</H3>

Select the date you wish to accomplish this goal by.

<FORM RUNAT="Server">

<ASP:CALENDAR ID="calendarGoal"

onSelectionChanged="Date_Selected" RUNAT="Server" /> <P/><ASP:TEXTBOX ID="inputMessage" RUNAT="Server" /> <P/><ASP:BUTTON TEXT=nSubmitDate" RUNAT="Server"/> <P/><ASP:RANGEVALIDATOR ID="rangeValDate" TYPE="Date" CONTROLTOVALIDATE="inputMessage" MAXIMUMVALUE=n1/1/2002" MINIMUMVALUE="1/1/1990"

TEXT="Please enter a date between 1/1/1990 and 1/1/2002."

RUNAT="Server"/>

RESULT:

The result is a page that contains a calendar server control for selecting a date. When the date is selected and then submitted, the date is validated on the server. If the date does not fall within the specified date range, a message is displayed to the user.

i| Untitled - Notepad

File Edit Format Help jsjx]

<FONT FACE ="Verdana">

<H3>Welcome to myllfetlmegoals.com</H3> Please help us by taking the following survey.

cFORM RUNAT-'Server"-

Whatlsthe date of the lasttlmeyouvlsltedwww.rnylifetlmegoals.com? <ASP:TEX.TBOX D-'InputLastDateVlsltedMyLlfetlmeGoals" TEXTMODE-'SlngleLlne" TEXT-"" WIDTH-'1 OOpx' RUNAT-'Server"/-<PI>

<ASP BUTTON TEXT-'Submit" RUNAT="Server"/>

<ASP: RANGE VALIDATOR ID="ranoeValDate" TYPE="Date"_

CONTROLTOVALIDATE-'InputLastDateVlsltedMyLlfetlmeGoals"

MAXIMUM VALUE-" 1/1/2002" MINIMUMVALUE-'1/1/1990" TEXT-'Please enter c date between 1/1/1990 and 1/1/2002." RUNAT="Server"/H_

Within the RangeValidator control, set the validation message by specifying the TEXT attribute.

• Save the file and request it from the Web server.

Note: See pages 20 to 25 for instructions on saving a file to the Web server and then requesting the file using the IIS Admin.

Fill in the form with a date outside the range.

-■ A validation message appears.

VALIDATE WITH REGULAR EXPRESSIONS

You may encounter situations where you have to be very specific about validating the text that a user inputs. For these situations, you can use the RegularExpressionValidator control. This control lets you use regular expressions for your criteria to determine whether the input is valid. Regular expressions are a pattern-matching language used for processing text. For more information on pattern matching, see Microsoft's MSDN site (msdn.microsoft.com).

Using the RegularExpressionValidator control is much like using the other validation controls. On the form containing the control you wish to validate, declare the control with the

<ASP:RegularExpressionValidator

RUNAT="Server"> tag. You then need to specify the control to validate with the controltovalidate attribute. Next, use the validationexpression attribute to indicate what you wish to use for the regular expression. Finally, specify the validation message using the text attribute.

For example, you can use a simple regular expression to ensure that the user has filled in a text box with a ZIP Code. In the regular expression, you can just check to make sure that five numbers where entered. If the text box does not contain five numbers, you can display a message back to the users and enable them to correct the input and resubmit the form for validation and processing.

VALIDATE WITH REGULAR EXPRESSIONS

VALIDATE WITH REGULAR EXPRESSIONS

D Open the

GenericTemplate.aspx template from the Code Templates directory.

0 Add a message to the user.

—0 Add a message to the user and TextBox control to the form.

Q Add the Regular ExpressionValidator control to the page.

D Open the

GenericTemplate.aspx template from the Code Templates directory.

0 Add a message to the user.

—0 Add a message to the user and TextBox control to the form.

^0 Add a Button control to the form.

Q Add the Regular ExpressionValidator control to the page.

WORK WITH WEB CONTROLS

Regular Expressions are commonly used for validating fields. Here are some useful examples of Regular Expressions.

CODE

DESCRIPTION

\d{3}-\d{2}-\d{4}

Testing for a valid Social Security Number.

\d{5}(-\d{4})?

Testing for valid US postal code.

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}

Testing for a valid US phone number.

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}

Testing for a valid e-mail address.

http://([\w-]\.)+[\w-](/[\w- ./?%=]*)?

Testing for a valid URL.

\d{4}-?\d{4}-?\d{4}-?\d{4}

Testing for Visa Credit Card.

L-Q Within the RegularExpressionValidator control, set the validation message by specifying the TEXT attribute.

• Save the file and request it from the Web server.

L-Q Within the RegularExpressionValidator control, set the validation message by specifying the TEXT attribute.

• Save the file and request it from the Web server.

Note: See pages 20 to 25 for instructions on saving a file to the Web server and then requesting the file using the IIS Admin.

I Type in an invalid zip code.

I A message appears.

Was this article helpful?

0 0

Post a comment