开发者

How can i check for character , number and special characters in a string?

开发者 https://www.devze.com 2023-02-05 19:30 出处:网络
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.

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

精彩评论

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