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

22,060

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.

Share:
22,060
Preeti
Author by

Preeti

i am software developer.i love to learn new things. SOreadytohelp : This website really helped me to resolve my issues and improved my programming skills.

Updated on January 22, 2020

Comments

  • Preeti
    Preeti over 4 years

    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.

  • Saggio
    Saggio over 13 years
    I forgot about Regex's; Joel's answer is much more straightforward than mine :p
  • Goose
    Goose over 8 years
    For anyone learning VB like I am, to use this, you may need to import Regex by placing this at the top of the file. Imports System.Text.RegularExpressions