[英]How can i check for character , number and special characters in a string?
我想讓用戶在文本框中僅輸入數字和字符,即沒有特殊字符。 我不想使用文本框的按鍵事件。
因為我需要在gridview中相同的驗證。
所以我想驗證整個字符串。
提前致謝。
將Regex類用於正則表達式可以使用:
If Regex.IsMatch(myString, "^[A-Za-z0-9]+$") Then
'Do stuff
End If
編輯:我忘記添加^
和$
來表示匹配應該從開始到結束在字符串上。 如果允許空格,則還需要在其中放置\\s
。
您可以解析字符串,然后檢查ascii值以確保它們僅是字母數字。 這是一些偽代碼:
StrLength = Len(Text)
For x = 1 To StrLength
sChar = Mid$(Text, x, 1)'Gets the x'th charcter in Text
bASCII = Asc(sChar) 'Gets ASCII value of character
if bASCII(not in Range) Then ERROR
Next x
這是指向“ Ascii值”的鏈接: http : //www.asciitable.com/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.