ASP用正则验证邮箱地址手机号码电话号码格式

经常需要验证邮箱地址,手机号码,电话号码等。在这儿把ASP中的正则验证实现记录一下

Function validate(ByVal str,ByVal number)

Dim temp,reg

Set reg = new regexp

reg.ignorecase=true

reg.global=true

Select Case CStr(number)

' 英文+空格

Case "0" temp = "^[a-zA-Z ]+$"

' 数字+横杠

Case "1" temp = "^[0-9\-]+$"

' 半角数字

Case "2" temp = "^\d+$"

' 邮箱地址

Case "3" temp = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"

' 手机号码格式

Case "4" temp = "^(((13[0-9]{1})|159|153)+\d{8})$"

' 电话号码格式1

Case "5" temp = "^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$"

' 电话号码格式2

Case "6" temp = "^(([0\+]\d{2,3}-)?(0\d{2,3}))?(\d{7,8})(-(\d{3,}))?$"

Case Else temp = number

End Select

reg.pattern = temp

validate = reg.test(Trim(str))

Set reg = Nothing

End Function

条用方法:

if validate(tel,4) or validate(tel,5) or validate(tel,6) then

else

response.Write("<script language='javascript'>alert('请检查您的联系电话或者手机号码格式!'); history.back()</script>")

response.End()

end if

你可能感兴趣的:(asp)