ASP.NET揭秘读书笔记3

78 篇文章 0 订阅
Validating E-Mail Addresses

One of the most common and difficult validation tasks that arise when performing form validation is validating an e-mail address. This task is actually much more difficult than you might assume because the e-mail standard is so complicated.

NOTE

You can find the specification for e-mail addresses in RFC 822, Standard for ARPA Internet Text Messages, at the following location:

 
ftp://ftp.rfc-editor.org/in-notes/rfc822.txt

Even if a perfect e-mail validation regular expression is beyond your grasp, however, you can still hope to validate simple e-mail addresses. For example, you can use the following regular expression to check that an e-mail address starts with one or more nonwhitespace characters, followed by an @ sign, followed by one or more nonwhitespace characters, followed by a period, followed by one or more nonwhitespace characters:

 
/S+@/S+/./S+

So, this regular expression would match the following e-mail addresses:

steve@somewhere.com

steve@host.somewhere.com

steve_smith@somewhere.com

However, it would fail to match e-mail addresses like

 
steve@aol
steve smith@aol

which is what you want.

If you want to exclude all e-mail addresses that do not end with a top-level domain name between two and three characters, such as .com, .net, and .ws, you would use this expression:

 
/S+@/S+/./S{2,3}

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

Listing 3.5 RegularExpressionValidatorEmail.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>RegularExpressionvalidatorEmail.aspx</title></head>
<body>

<form Runat="Server">

Email Address:
<br>
<asp:TextBox
  id="txtEmail"
  Columns="50"
  Runat="Server"/>

<asp:RegularExpressionValidator
  ControlToValidate="txtEmail"
  Text="Invalid Email Address!"
  ValidationExpression="/S+@/S+/./S{2,3}"
  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 Usernames and Passwords

Typically, Web sites require you to enter a username and password containing only alphanumeric characters or the underscore character. You can perform this type of validation using a regular expression that looks like this:

 
/w+

This regular expression matches any expression that contains one or more word characters (a word character can be a letter, number, or the underscore character).

You also can specify a minimum and maximum length for a password by using a regular expression that looks like this:

 
/w{8,20}

This regular expression matches only expressions that are between 8 and 20 characters long.

Finally, some Web sites require you to use at least one number and one letter in your password. You can use the following regular expression to satisfy this requirement:

 
[a-zA-Z]+/w*/d+/w*

This regular expression requires you to enter at least one letter, followed by any number of word characters, followed by at least one number, followed by any number of word characters.

Listing 3.6 demonstrates how you can combine two RegularExpressionValidator controls and a RequiredFieldValidator control to validate a password. The Validation controls require you to enter a password that starts with at least one letter and contains one number and between 3 and 20 characters (special characters, such as # and ?, are not allowed).

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值