jimbona27
asked on
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,
Can I have a regular expression example in C# (ASP.NET) that checks a string to be a alphanumeric please?
Thanks,
(^-?[a-zA-Z0-9]*$)
this should do it
this should do it
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
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
(^-?[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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually, for my last post, a slight modification would be needed to validate that the string was alpha-numeric:
[^a-zA-Z0-9]+
[^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>
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.
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.
use @"\w+" to string escape it.
P.
P.
\w
also matches underscore, which the OP may not want :)
also matches underscore, which the OP may not want :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My previous post contains error in some input samples.
Try the below expression:
^\\w+[^_]$
Hope this will work.
-Shahan
Try the below expression:
^\\w+[^_]$
Hope this will work.
-Shahan
^\\w+[^_]$
will match:
abc_def
_1
will match:
abc_def
_1
@ 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
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
@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.
I was thinking at the time that RegularExpressionValidator
@ kaufmed
In your post, 30658606, your approach seems to be correct but still getting unexpected result.
In your post, 30658606 you are right mistake is from my side.
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.
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.
^[A-Za-z0-9]+$