简体   繁体   中英

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM