Solved

Regular Expression Question

Posted on 2006-07-11
6
399 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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