#######################
#作者:雨浪 版權所有,翻版說一下 #
#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%>