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.Is Match(strT oCheck);
}
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!
public bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
return !objAlphaNumericPattern.Is
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.IsM atch(strTo Check);
}
note:
\w is short hand for [A-Za-z]
\d ' ' [0-9]
\s ' ' white space
^[\w\d\s]+$
and don't negatate the returned bool:
public bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern = new Regex("^[\w\d\s]+$");
return objAlphaNumericPattern.IsM
}
note:
\w is short hand for [A-Za-z]
\d ' ' [0-9]
\s ' ' white space
sorry typo... by negatate i mean negate
Hi Dear
Check this
public bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9 ]");
return !objAlphaNumericPattern.Is Match(strT oCheck);
}
Check this
public bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9 ]");
return !objAlphaNumericPattern.Is
}
@MuhammadAdil: I already wrote this answer.
/^[a-zA-Z0-9]+$/