Apr 15

ASP 正则表达式验证及常用正则验证函数RegTest()

假设为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%>



附上一个用来测试正则的常用函数RegTest()

 

 程序代码
<%
Function RegTest(sPattern, sString)
    Dim regEx
    Set regEx = New RegExp
    regEx.Pattern = sPattern
    regEx.Global = True
    regEx.IgnoreCase = False
    RegTest = regEx.Test(sString)
End Function
%>

等等,下面还有精彩的文章哟:

to "ASP 正则表达式验证及常用正则验证函数RegTest()"

Leave a Reply