Regex驗證

來源:互聯網
上載者:User


#######################
#作者:雨浪 著作權,翻版說一下     #
#QQ:270499458         #
#######################

近段日子幾個剛學了Regex的朋友問我在asp中怎麼用.呵呵.雖然簡單,還是寫出來吧,Regex的基本知識我就不說了.其實已經有很多這樣的文章了.:(

#####函數代碼########
假設為myfunc.asp

<%
'正則表運算式驗證函式 patrn-Regex strng-需要驗證的字串
Function RegExpTest(patrn, strng)
Dim regEx, retVal ' 建立變數。
Set regEx = New RegExp ' 建立Regex。
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 '建立變數,正確或者失敗標記
'大家注意哦,順便我在這裡貢獻一個Regex,同時驗證電話號碼和手機號碼的!
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%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.