VBSCRIPTRegex驗證使用者名稱函數

來源:互聯網
上載者:User

<%

Function IsValidUserName(UserName)
IsValidUserName = True
'判斷使用者名稱長度是否在3-20字元之間
If   Len(UserName)<3   or   Len(UserName)>20   Then  
      IsValidUserName=False  
      Exit   Function  
End   If  
'檢測是否包含特殊字元,可能造成不安全的SQL注入
For i = 1 To Len(UserName)
c = Lcase(Mid(UserName, i, 1))
If InStr("$!<>?#^%@~`&*();:+='"" ", c) > 0 Then
IsValidUserName = False
Exit Function
End IF
Next
'判斷輸入的使用者名稱是否是中文字元,英文字元(大小寫),數字,底線,中文字元組合
Dim regEx, Match1, Matches      ' 建立變數。
Set regEx = New RegExp         ' 建立Regex。
regEx.Pattern ="[^0-9a-z_/u4e00-/u9fa5]"        ' 設定模式。
regEx.IgnoreCase = True         ' 設定是否區分字元大小寫。
regEx.Global = True         ' 設定全域可用性。
Set Matches = regEx.Execute(UserName)   ' 執行搜尋。
For Each Match1 in Matches      ' 遍曆匹配集合。
RetStr = RetStr & "找到非法字元位置:" & Match1.FirstIndex & " 字元是:"& Match1.Value & "" & vbCRLF
Next
response.Write("<br>"&RetStr)
if RetStr="" then
IsValidUserName = True
else
IsValidUserName = False
end if
RegExpTest = RetStr

End Function
response.Write(IsValidUserName("我是_xqf222"))
response.Write(IsValidUserName("我是_xqf   222"))

%> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.