Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Regular Expression Question

Posted on 2006-07-11
Medium Priority
418 Views
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!
0
Question by:marmaxx
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 20

Accepted Solution

TheAvenger earned 1200 total points
ID: 17079388
Just use the expression:

[^a-zA-Z0-9 ]

I.e. add a space at the end.
0

LVL 7

Expert Comment

ID: 17079754

/^[a-zA-Z0-9]+\$/
0

LVL 18

Expert Comment

ID: 17080116
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
0

LVL 18

Expert Comment

ID: 17080125
sorry typo... by negatate i mean negate
0

LVL 6

Expert Comment

ID: 17080596
Hi Dear

Check this

public bool IsAlphaNumeric(String strToCheck)
{

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

return !objAlphaNumericPattern.IsMatch(strToCheck);
}
0

LVL 20

Expert Comment

ID: 17080626
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
###### Suggested Courses
Course of the Month11 days, 13 hours left to enroll