正则表达式验证

 

#######################

  #作者:雨浪 版权所有,翻版说一下     #

  #QQ:270499458         #

  #######################

  近段日子几个刚学了正则表达式的朋友问我在asp中怎么用.呵呵.虽然简单,还是写出来吧,正则表达式的基本知识我就不说了.其实已经有很多这样的文章了.:(

  #####函数代码########

  假设为myfunc.asp

  <%

  '正则表表达式验证函数 patrn-正则表达式 strng-需要验证的字符串

  Function RegExpTest(patrn, strng)

  Dim regEx, retVal ' 建立变量。

  Set regEx = New RegExp ' 建立正则表达式。

  regEx.Pattern = patrn ' 设置模式。

  regEx.IgnoreCase = False ' 设置是否区分大小写。

  retVal = regEx.Test(strng) ' 执行搜索测试。

  RegExpTest = retVal '返回不尔值,不符合就返回false,符合为true

  End Function

  %>

  #####提交页面代码######

  假设为mypage.asp

  <form method="post" action="check.asp">

  请输入E-mail地址:<input type=text name=email>

  <br>

  请输入电话号码:<input type=text name=tel>

  <input type=submit value="确定">

  </form>

  #####验证页面########

  假设为check.asp

  <!--#include file="myfunc.asp"-->

  <%

  tel=request.form("tel")

  email=request.form("email")

  dim founderr : founderr=false '建立变量,正确或者失败标记

  '大家注意哦,顺便我在这里贡献一个正则表达式,同时验证电话号码和手机号码的!

  if RegExpTest("(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)", tel)=false then

  founderr=true

  regshow=regshow&"<li>您输入的电话号码格式不正确"

  end if

  if RegExpTest("^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$", email)=false then

  founderr=true

  regshow=regshow&"<li>您输入的电子邮箱格式不正确"

  end if

  if founderr=false then regshow="<li>您输入的格式都是正确的哦"

  %>

  <br><br>

  <%=regshow%>