alphanumeric regular expression needed

hi EE,
Can I have a regular expression example in C# (ASP.NET) that checks a string to be a alphanumeric please?
Thanks,
jimbona27Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
Use a pattern like this:

^[A-Za-z0-9]+$
brad2575Commented:
(^-?[a-zA-Z0-9]*$)

this should do it
Patrick MatthewsCommented:
jimbona27,

Please clarify what you mean by "alphanumeric".

For example, my pattern will reject the following:

<zero length string>
-
-1
-a
-234xyz

brad2575's pattern will accept all of them...

Patrick
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

lucky_jamesCommented:
(^-?[a-zA-Z0-9]*$) should be the one.

For more about regular expressions, check out:
http://www.regular-expressions.info

It contains a similar example as well. Check out:
http://www.regular-expressions.info/numericramges.html

Hope it helps.
käµfm³d 👽Commented:
C# example (with credit to matthewspatrick for the pattern):
public bool IsAlphaNum(string input)
{
    return System.Text.RegularExpressions.Regex.IsMatch(input, "^[a-zA-Z0-9]+$");
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
P.S.

Since you mentioned this was for ASP, you could also use matthewspatrick's pattern inside of a RegularExpressionValidator object's ValidationExpression field to validate a control's text.

Example:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
    ErrorMessage="Please enter an alpha-numeric string" ControlToValidate="TextBox1" 
    ValidationExpression="^[a-zA-Z0-9]+$"></asp:RegularExpressionValidator>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

Open in new window

käµfm³d 👽Commented:
Actually, for my last post, a slight modification would be needed to validate that the string was alpha-numeric:

[^a-zA-Z0-9]+
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
    ErrorMessage="Please enter an alpha-numeric string" ControlToValidate="TextBox1" 
    ValidationExpression="[^a-zA-Z0-9]+"></asp:RegularExpressionValidator>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

Open in new window

Pui_YunCommented:
Hi jimbona27,
You can also use "\w+" to check that all characters are alphanumeric.

This is a tutorial that I found very helpful:
http://www.codeproject.com/KB/dotnet/regextutorial.aspx

You can also download the expresso regex application which is based on .NET technology (i.e. like c#).

Hope this helps.
P.
Pui_YunCommented:
use @"\w+" to string escape it.
P.
käµfm³d 👽Commented:
\w

also matches underscore, which the OP may not want  :)
Shahan AyyubSenior Software EngineerCommented:
Try this one,

add this namespace:
using System.Text.RegularExpressions;


'output' will be set to 'TRUE' or 'False'


hope this will help
-Shahan
string str = "1234abcd";
            Regex re = new Regex(@"([0-9]([a-z]))+|([a-z]([0-9]))+",RegexOptions.IgnoreCase);
            string output = re.IsMatch(str).ToString();

Open in new window

Shahan AyyubSenior Software EngineerCommented:
My previous post contains error in some input samples.

Try the below expression:

^\\w+[^_]$

Hope this will work.



-Shahan
käµfm³d 👽Commented:
^\\w+[^_]$

will match:

    abc_def
    _1
Shahan AyyubSenior Software EngineerCommented:
@ kaufmed

You posted this regular expression & ASP.NET code for its use
[^a-zA-Z0-9]+


  1) ASDV09asd
       this is treated as non-alphanumeric. I could not understand ????

I was testing the patterns from here:

http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
käµfm³d 👽Commented:
@Shahan_Developer

I was thinking at the time that RegularExpressionValidator displayed its error message if the pattern was successful, not if it failed. It seems the latter is correct--good catch Shahan_Developer. The first post, 30658606, would be the proper usage.
Shahan AyyubSenior Software EngineerCommented:

@ kaufmed

In your post, 30658606, your approach seems to be correct but still getting unexpected result.
Shahan AyyubSenior Software EngineerCommented:
In your post, 30658606 you are right mistake is from my side.
Pui_YunCommented:
Thanks kaufmed,
I didn't know that the underscore was treated as part of the alphanumeric type, anyhow, I just wanted to post the corrected other way for matching:

@"^[\w-[_]]+$"

This will work too.
P.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.