登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

樱之花

叶散的时候,你明白欢聚;花谢的时候,你明白青春.

 
 
 

日志

 
 
关于我

分类中“我的实验室”是我在日常工作中的一些知识总结,有些写的比较匆忙,可能大家在阅读时会产生困扰,后期有时间我会重新整理编辑,谢谢大家的到访,您们的支持是我前进的动力!

ASP.Net表单验证快速代码  

2009-08-09 22:40:57|  分类: .NET/C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

http://hi.baidu.com/redshirt/blog/item/3196fb24fe325a358644f92a.html

 

1、验证数字及字母

e.g. only if you enter a product code that starts with the uppercase letter P and contains no more, or no fewer, than four numerals in a row. The RegularExpressionValidator control uses the regular expression P[0-9]{4}.

<asp:RegularExpressionValidator

   ControlToValidate="txtNumber"

   Text="Invalid Code!"

   ValidationExpression="P[0-9]{4}"

   Runat="Server" />

----------------------------------------------------------------

2、验证电子邮件地址

e.g. 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.

<asp:RegularExpressionValidator

   ControlToValidate="txtEmail"

   Text="Invalid Code!"

   ValidationExpression="\S+@\S+\.\S+"

   Runat="Server" />

------------------------------------------------------------

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

<asp:RegularExpressionValidator

   ControlToValidate="txtXiaoZ"

   Text="Invalid Code!"

   ValidationExpression="\S+@\S+\.\S{2,3}"

   Runat="Server" />

3、验证用户名和密码

e.g. 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).

<asp:RegularExpressionValidator

   ControlToValidate="txtXiaoZ"

   Text="Invalid Code!"

   ValidationExpression="\w+"

   Runat="Server" />

-----------------------------------------------------------

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

<asp:RegularExpressionValidator

   ControlToValidate="txtXiaoZ"

   Text="Invalid Code!"

   ValidationExpression="\w{8,20}"

   Runat="Server" />

----------------------------------------------------------------

e.g. 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.

<asp:RegularExpressionValidator

   ControlToValidate="txtXiaoZ"

   Text="Invalid Code!"

   ValidationExpression="[a-zA-Z]+\w*\d+\w*"

   Runat="Server" />

----------------------------------------------------------------

4、验证电话号码

e.g.

(555) 555-5555

555.555.5555

555 555-555

<asp:RegularExpressionValidator

   ControlToValidate="txtPhone"

   Display="Dynamic"

   Text="Invalid Phone Number!"

   ValidationExpression="\(?\s*\d{3}\s*[\)\.\-]?\s*\d{3}\s*[\-\.]?\s*\d{4}"

   Runat="Server" />

--------------------------------------------------------------------

5、验证互联网web地址

e.g. This regular expression matches any string that begins with the characters ASP.Net表单验证快速代码 - IDesire - YinZhiHua2008 http:// followed by one or more nonwhitespace characters, followed by a period, followed by one or more nonwhitespace characters.

<asp:RegularExpressionValidator

   ControlToValidate="txtHomepage"

   Display="Dynamic"

   Text="Invalid URL!"

   ValidationExpression="http://\S+\.\S+"

   Runat="Server" />

-------------------------------------------------------------------

6、验证输入字符长度

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

<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" />

-------------------------------------------------------------------

7、验证邮政编码

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

<asp:RegularExpressionValidator

   ControlToValidate="txtZipCode"

   Display="Dynamic"

   Text="Invalid ZIP Code!"

   ValidationExpression="\d{5}"

   Runat="Server" />

  评论这张
 
阅读(1277)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018