Solved

Regular Expression Question

Posted on 2006-07-11
6
406 Views
Last Modified: 2008-02-01
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
Comment
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
  • Learn & ask questions
6 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 300 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

by:pradeepsudharsan
ID: 17079754

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

Expert Comment

by:Ravi Singh
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17080125
sorry typo... by negatate i mean negate
0
 
LVL 6

Expert Comment

by:MuhammadAdil
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

by:TheAvenger
ID: 17080626
@MuhammadAdil: I already wrote this answer.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question