ASP.NET揭秘读书笔记4

78 篇文章 0 订阅
Listing 3.6 RegularExpressionValidatorPassword.aspx
<Script Runat="Server">

Sub Button_Click( s As Object, e As EventArgs )
  If IsValid Then
    Response.Redirect( "thankyou.aspx" )
  End If
End Sub

</Script>

<html>
<head><title>RegularExpressionValidatorPassowrd.aspx</title></head>
<body>

<form Runat="Server">

Password:
<br>
<asp:TextBox
  id="txtPassword"
  Columns="30"
  Runat="Server"/>

<asp:RequiredFieldValidator
  ControlToValidate="txtPassword"
  Display="Dynamic"
  Text="You must enter a password!"
  Runat="Server" />

<asp:RegularExpressionValidator
  ControlToValidate="txtPassword"
  Display="Dynamic"
  Text="Your password must contain between 3 and 20 characters!"
  ValidationExpression="/w{3,20}"
  Runat="Server" />

<asp:RegularExpressionValidator
  ControlToValidate="txtPassword"
  Display="Dynamic"
  Text="Your password must contain at least one number and letter!"
  ValidationExpression="[a-zA-Z]+/w*/d+/w*"
  Runat="Server" />
<p>
<asp:Button
  Text="Submit"
  OnClick="Button_Click"
  Runat="Server"/>

</form>
</body>
</html>

The C# version of this code can be found on the CD-ROM.

Validating Phone Numbers

Phone numbers are difficult to validate—especially when you take into consideration foreign area codes and phone number extensions. Even if you ignore these problems and concentrate on U.S. phone numbers without extensions, many different formats still are used when entering a phone number. For example, the following formats are all commonly used:

 
(555) 555-5555
555.555.5555
555 555-555

You can create a regular expression that matches all three of the preceding expressions, as follows:

 
/(?/s*/d{3}/s*[/)/./-]?/s*/d{3}/s*[/-/.]?/s*/d{4}

The page in Listing 3.7 illustrates how you can use this regular expression with RegularExpressionValidator.

Listing 3.7 RegularExpressionValidatorPhone.aspx
<Script Runat="Server">

Sub Button_Click( s As Object, e As EventArgs )
  If IsValid Then
    Response.Redirect( "ThankYou.aspx" )
  End If
End Sub

</Script>

<html>
<head><title>RegularExpressionValidatorPhone.aspx</title></head>
<body>

<form Runat="Server">

Phone Number:
<br>
<asp:TextBox
  id="txtPhone"
  Columns="30"
  Runat="Server"/>

<asp:RegularExpressionValidator
  ControlToValidate="txtPhone"
  Display="Dynamic"
  Text="Invalid Phone Number!"
  ValidationExpression="/(?/s*/d{3}/s*[/)/./-]?/s*/d{3}/s*[/-/.]?/s*/d{4}"
  Runat="Server" />
<p>

<asp:Button
  Text="Submit"
  OnClick="Button_Click"
  Runat="Server"/>

</form>
</body>
</html>

The C# version of this code can be found on the CD-ROM.

Validating Web Addresses

You also might need to validate URLs that users enter into a form at your Web site. For example, you might have a registration form that contains a field for the URL of a user's home page.

You can use the following regular expression to check for a valid URL:

http:///S+/./S+

This regular expression matches any string that begins with the characters http:// followed by one or more nonwhitespace characters, followed by a period, followed by one or more nonwhitespace characters.

The page in Listing 3.8 demonstrates how you can use this regular expression with RegularExpressionValidator.

Listing 3.8 RegularExpressionValidatorWeb.aspx
<Script Runat="Server">

Sub Button_Click( s As Object, e As EventArgs )
  If IsValid Then
    Response.Redirect( "thankyou.aspx" )
  End If
End Sub

</Script>

<html>
<head><title>RegularExpressionValidatorWeb.aspx</title></head>
<body>

<form Runat="Server">

Enter the address
of your homepage:
<br>
<asp:TextBox
  id="txtHomepage"
  Columns="50"
  Runat="Server"/>

<asp:RegularExpressionValidator
  ControlToValidate="txtHomepage"
  Display="Dynamic"
  Text="Invalid URL!"
  ValidationExpression="http:///S+/./S+"
  Runat="Server" />

<p>

<asp:Button
  Text="Submit"
  OnClick="Button_Click"
  Runat="Server"/>

</form>
</body>
</html>

The C# version of this code can be found on the CD-ROM.

One problem with the code in Listing 3.8 concerns case-sensitivity. The RegularExpressionValidator control uses case-sensitive comparisons. So, the regular expression discussed in this section matches

http://www.superexpert.com

but does not match

HTTP://www.superexpert.com

Unfortunately, RegularExpressionValidator does not have a property that you can set to enable regular expression options such as the i option (an option which enables case-insensitive matches). You cannot use the i option inline because the syntax for doing so with JavaScript is different from the syntax for doing so with the .NET classes.

A not completely satisfactory workaround to this problem is illustrated by the page in Listing 3.9. The RegularExpressionValidator in this page performs a case insensitive match by using the i option. Furthermore, the RegularExpressionValidator disables client-side validation to prevent conflicts with JavaScript.

This is not a perfect workaround to the problem since the page must be submitted back to the server before the RegularExpressionValidator will validate the expression.

Listing 3.9 RegularExpressionIgnoreCase.aspx
<Script Runat="Server">

Sub Button_Click( s As Object, e As EventArgs )
  If IsValid Then
    Response.Redirect( "thankyou.aspx" )
  End If
End Sub

</Script>

<html>
<head><title>RegularExpressionValidatorIgnoreCase.aspx</title></head>
<body>

<form Runat="Server">

Enter the address
of your homepage:
<br>
<asp:TextBox
  id="txtHomepage"
  Columns="50"
  Runat="Server"/>

<asp:RegularExpressionValidator
  ControlToValidate="txtHomepage"
  Display="Dynamic"
  Text="Invalid URL!"
  EnableClientScript="False"
  ValidationExpression="(?i:http:///S+/./S+)"
  Runat="Server" />

<p>

<asp:Button
  Text="Submit"
  OnClick="Button_Click"
  Runat="Server"/>

</form>
</body>
</html>

The C# version of this code can be found on the CD-ROM.

Checking for Entry Length

You also can use RegularExpressionValidator to check whether a form field contains more than a certain number of characters. This type of validation is especially useful when you're working with MultiLine TextBox controls. Because a MultiLine TextBox control does not have a MaxLength property, there is nothing to prevent a user from typing any number of characters in the text box.

To check for a certain entry length, use regular expression quantifiers like this:

 
.{0,10}

This expression matches any entry (including spaces) that contains between 0 and 10 characters.

If you want to restrict the entry to a string of nonwhitespace characters that has a certain length, you would use a regular expression that looks like this:

 
/S{0,10}

The page in Listing 3.10 demonstrates how you can use this regular expression with RegularExpressionValidator.

Listing 3.10 RegularExpressionValidatorLength.aspx
<Script Runat="Server">

Sub Button_Click( s As Object, e As EventArgs )
  If IsValid Then
    Response.Redirect( "ThankYou.aspx" )
  End If
End Sub

</Script>

<html>
<head><title>RegularExpressionValidatorLength.aspx</title></head>
<body>

<form Runat="Server">

Enter your last name:
<br>(no more than 10 characters)
<br>
<asp:TextBox
  id="txtLastname"
  Columns="50"
  Runat="Server"/>

<asp:RegularExpressionValidator
  ControlToValidate="txtLastname"
  Display="Dynamic"
  Text="Your last name can contain
        a maximum of 10 characters and no spaces!"
  ValidationExpression="/S{0,10}"
  Runat="Server" />
<p>

<asp:Button
  Text="Submit"
  OnClick="Button_Click"
  Runat="Server"/>

</form>

</body>
</html>

The C# version of this code can be found on the CD-ROM.

Validating ZIP Codes

You also can use the RegularExpressionValidator control to validate ZIP codes. For example, if you want to require that a ZIP code entered into a form field contains exactly five digits, you would use the following regular expression:

 
/d{5}

This expression matches strings that have no more or no fewer than five digits.

The page in Listing 3.11 demonstrates how you would use this regular expression with the RegularExpressionValidator control.

Listing 3.11 RegularExpressionValidatorZip.aspx
<Script Runat="Server">

Sub Button_Click( s As Object, e As EventArgs )
  If IsValid Then
    Response.Redirect( "ThankYou.aspx" ) 
  End If
End Sub

</Script>

<html>
<head><title>RegularExpressionValidatorZip.aspx</title></head>
<body>

<form Runat="Server">

ZIP Code:
<asp:TextBox
  id="txtZipCode"
  Columns="8"
  Runat="Server"/>

<asp:RegularExpressionValidator
  ControlToValidate="txtZipCode"
  Display="Dynamic"
  Text="Invalid ZIP Code!"
  ValidationExpression="/d{5}"
  Runat="Server" />

<p>

<asp:Button
  Text="Submit"
  OnClick="Button_Click"
  Runat="Server"/>

</form>

</body>
</html>

The C# version of this code can be found on the CD-ROM.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值