?
Solved

Regular Expression Question

Posted on 2006-07-11
6
Medium Priority
?
412 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 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

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 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