[英]How can i check for character , number and special characters in a string?
I want to user to enter only numbers and characters in textbox ie no special charaters. 我想让用户在文本框中仅输入数字和字符,即没有特殊字符。 I don't want to use key press event of textbox.
我不想使用文本框的按键事件。
As i need same validation in gridview. 因为我需要在gridview中相同的验证。
So i want to validate whole string. 所以我想验证整个字符串。
Thanks in advance. 提前致谢。
Using the Regex class for regular expressions you can use: 将Regex类用于正则表达式可以使用:
If Regex.IsMatch(myString, "^[A-Za-z0-9]+$") Then
'Do stuff
End If
EDIT: I forgot to add the ^
and the $
to denote that the match should go from start to finish on the string. 编辑:我忘记添加
^
和$
来表示匹配应该从开始到结束在字符串上。 You'll also need to put a \\s
in there if whitespace is allowed. 如果允许空格,则还需要在其中放置
\\s
。
You can parse the string and then check the ascii values to make sure they are only Alpha-numeric. 您可以解析字符串,然后检查ascii值以确保它们仅是字母数字。 Here's some pseudocode:
这是一些伪代码:
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
Here's a link for to the Ascii Values: http://www.asciitable.com/ 这是指向“ Ascii值”的链接: http : //www.asciitable.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.