C# .NET - Regular expression for accepting alphnumeric characters in C#.net

Asked By viji selvaraj on 16-Jun-10 08:51 AM
I have a requirement to validate user entered Text fields. Validation requirement is as below.
  1. It should be alphanumeric (a-z , A-Z , 0-9)
  2. To allow special characters like Space, +, /,-, _, (,).
  3. It allow Only Characters

Can u Please any one help me?


Sandra Jain replied to viji selvaraj on 16-Jun-10 08:54 AM
^[A-Za-z0-9]*$    would match zero or more...     
^[A-Za-z0-9]+$ would match one or more...
Anoop S replied to viji selvaraj on 16-Jun-10 09:13 AM
To accept only alphanumeric characters, use the validation expression as ^[0-9a-zA-Z]+$
To allow special characters only ^[, +, /,-, _, (,)]+$
refer this for more information about regular expression
http://msdn.microsoft.com/en-us/library/ms972966.aspx
Sagar P replied to viji selvaraj on 16-Jun-10 09:20 AM
Normally we have regular expression for Alphnumeric like;

^[a-zA-Z0-9]*$

If you want to include some special chanracter then you can include it like; if you want underscore then you can use regualr expression like this;

^[a-zA-Z0-9_]*$

But if you want all special characters then i think nothing remains on keyboard which you dont want to allow.....

Goniey N replied to viji selvaraj on 16-Jun-10 09:52 AM
Use This Function :

// Function to Check for AlphaNumeric.
public bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern=
new Regex("[^a-zA-Z0-9]");
return !objAlphaNumericPattern.IsMatch(strToCheck); 
}




// Check Special Characters "+, /, -, (, ), ., _". If You Want To Use "\" The Use "\\" Because It Is Special Character.
Regex RegexPattern=new Regex("^[+,/,-,(,),.,_,//]+$");


//I think This Will Very Helpful To You....
Timothy Power replied to viji selvaraj on 16-Jun-10 01:57 PM
Based on the responses I've seen so far the only problem your going to have is with the character "-". Regular expressions have metacharaters that have special meanings like ^ begining of line, $ End of Line, + One to many etc ...
So whenever you have to look for the literal value of a metacharater you have to escape like this, \(, \+, \? etc ...
When ever you enclose characters in brackets like so [aeiou] the regular expression engine will look for occurances of each character individually, furthermore when metacharacters are placed inside brackets they don't have to be escaped with three exceptions:

  * the - character, this metacharacter means through like a-z, 0-9, and if you know your ascii chart !-~ which means
    every character on the keyboard. If you want to look for the literal "-" it must be escaped like this \-

  * Now that you understand the value of "\", surely you'll understand the importance of its position in the brackets. 
    You don't want to escape a metacharacter if you're looking for its literal value.

  * As I stated above ^ means begining of string in regex; however, it means something totally different when used in 
    brackets. If it is the first character in the brackets, you're telling the regex engine to return true if it finds any 
    character that is not in your brackets.

bracket notation already tells regex to look for each character individually so you don't need the redundant comma usage; however, if you're looking for a comma make sure you've got one in there.

Timothy Power replied to viji selvaraj on 16-Jun-10 02:53 PM
One last thing, the pattern "[ -~]" will include only the chacters you can see with your keyboard. If you can google an ascii chart you'll understand how this works and how you can modify it to do future seaches. This pattern also excludes new line, carriage return, and control chars all together.

  1. It should be alphanumeric (a-z , A-Z , 0-9)                 @"[a-zA-Z0-9]*"
  2. To allow special characters like Space, +, /,-, _, (,).      @"[+,/\-\\_(){}[]~!@#$%^&*?<>';:\"]*"
  3. It allow Only Characters                             @"[ -~]"

The way you phrased you question defines what it can look like but not what it must look like in the future you might want to write similiar code but demands a quantity. Concider the following expression:

  string myStrongPasswordPattern = @"^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#\$%\^&;\+=\*!\-_\(\)\?]).*$";

This pattern demands that the string have 

  at least 6 chars in length
  at least 1 digit
  at least 1 Uppercase letter
  at least 1 Lowercase letter
  at least 1 character defined by the last grouping in the pattern => ])(?=.*[@#\$%\^&;\+=\*!\-_\(\)\?]), between the 
  brackets is where you define exactly which special characters to allow.

Hope this helps