开发者

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 i.e no special charaters. I don't want to use key press event of textbox.

As i need same开发者_开发问答 validation in gridview.

So i want to validate whole string.

Thanks in advance.


Using the Regex class for regular expressions you can use:

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.


You can parse the string and then check the ascii values to make sure they are only Alpha-numeric. 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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜