x
Solved

# Regular Expression Question

Posted on 2006-07-11
Medium Priority
423 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

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…
###### Suggested Courses
Course of the Month8 days, 7 hours left to enroll