Link to home
Start Free TrialLog in
Avatar of marmaxx
marmaxx

asked on

Regular Expression Question

I am using the following function to test for valid alpha-numeric input:

public bool IsAlphaNumeric(String strToCheck)
{
    Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");

      return !objAlphaNumericPattern.IsMatch(strToCheck);
 }


The only problem with the expression is that it doesn't allow spaces between the words. Is there a way I can modify this expression to allow spaces?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pradeepsudharsan
pradeepsudharsan


/^[a-zA-Z0-9]+$/
try:

^[\w\d\s]+$

and don't negatate the returned bool:

public bool IsAlphaNumeric(String strToCheck)
{
      Regex objAlphaNumericPattern = new Regex("^[\w\d\s]+$");
      return objAlphaNumericPattern.IsMatch(strToCheck);
 }


note:
\w is short hand for [A-Za-z]
\d ' ' [0-9]
\s ' ' white space
sorry typo... by negatate i mean negate
Avatar of MuhammadAdil
Hi Dear

Check this

 public bool IsAlphaNumeric(String strToCheck)
        {

            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9 ]");

            return !objAlphaNumericPattern.IsMatch(strToCheck);
        }
@MuhammadAdil: I already wrote this answer.