Welcome to Dream.In.Code
Become a C# Expert!

Join 150,042 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,606 people online right now. Registration is fast and FREE... Join Now!




Regular expressions in ASP.net/C#

2 Pages V  1 2 >  
Reply to this topicStart new topic

Regular expressions in ASP.net/C#

kkgaming
4 Feb, 2008 - 07:37 PM
Post #1

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
I want a user to enter their username and have it be 5-8 characters long which would look like this right?

CODE
\w{5,8}


But, I also want the characters to be alpha only, which would be like this:

CODE
\w+


But when I add the two expressions together, and I enter a username like nathan5, it doesn't flag it as an error.

What expression would I use if not the two above? Or how do I combine the two?

CODE
      Username:
        <asp:TextBox ID="Text1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="Text1"
            ErrorMessage="Must be 5-8 characters long, alpha only" ValidationExpression="\w+\w{5,8}"></asp:RegularExpressionValidator><br />



This post has been edited by kkgaming: 4 Feb, 2008 - 07:42 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 08:01 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
try [a-zA-Z]{5,8}
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 08:06 PM
Post #3

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
Yup, I believe that worked, thanks. I also need a password 6-8 characters, start with letter, at least 1 number. I cam e up with this:

CODE
[a-zA-Z]{5,8}+\w*\d+\w*


But I am not sure how to have one letter first for the password....

This post has been edited by kkgaming: 4 Feb, 2008 - 08:16 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 08:37 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
try this

[a-zA-Z]+[0-9]+[a-zA-Z0-9]*
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 08:42 PM
Post #5

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
That works, but how do I get it to be 5-8 characters only....
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 08:46 PM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
well I'm not sure about this but:

([a-zA-Z]+[0-9]+[a-zA-Z0-9]*){5,8}

But as I said, I'm not sure.
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 08:50 PM
Post #7

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
hmmm, yeah, that flags a lot of stuff wrong that shouldn't be. I am not sure either...
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 09:20 PM
Post #8

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
OK just another thought

([a-zA-Z]\w{3,6}\d)|([a-zA-Z]\d\w{3,6})
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 10:13 PM
Post #9

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
yup, that works, but I meant 6,8 not 5,8. Sorry for telling you wrong that does work though. What would it be for 6,8? sorry told you wrong.

I think it would be: (judging by what you did)

CODE
([a-zA-Z]\w{4,6}\d)|([a-zA-Z]\d\w{4,6})


However, when I start the password with a letter and have a number in the middle of letters it flags it as an error...It works otherwise..


thanks again, for the help.

This post has been edited by kkgaming: 4 Feb, 2008 - 10:27 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 10:18 PM
Post #10

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Yes, I believe that is correct, but try all the variants that you can think of, it's just a simple finite state automaton wink2.gif

But some how I still have doubts, anyways at least you get the idea.

This post has been edited by PennyBoki: 4 Feb, 2008 - 10:19 PM
User is offlineProfile CardPM
+Quote Post

kkgaming
RE: Regular Expressions In ASP.net/C#
4 Feb, 2008 - 10:28 PM
Post #11

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
Yeah, actually I think it doesn't work quite right. When I have a number within letters it flags it as an error. hmm, it works other than that though. I will try to figure it out, not unless you have another idea.
User is offlineProfile CardPM
+Quote Post

orcasquall
RE: Regular Expressions In ASP.net/C#
5 Feb, 2008 - 06:24 AM
Post #12

D.I.C Head
Group Icon

Joined: 14 Sep, 2007
Posts: 158



Thanked: 3 times
Dream Kudos: 50
My Contributions
Perhaps you can try this
CODE
(?=^[a-zA-Z0-9]{6,8}$)[a-zA-Z][a-zA-Z0-9]{0,6}\d[a-zA-Z0-9]{0,6}


Let's look at this part
CODE
^[a-zA-Z0-9]{6,8}$

This looks for an alphanumeric sequence of characters length 6 to 8. Carat ^ to match the start, dollar sign to match the end.

Then it's placed in (?= ... ) for a lookahead match. It'll take more to explain it. Just go with it...

Then you have [a-zA-Z] to match your condition of "must start with alphabet". The next part might be a little complicated to explain. Let's go with the 2 extreme examples first:
"a1bcdefg" and "abcdefg1", representing alpha-numeral-rest_are_alphanumeric and alpha-rest_are_alphanumeric-numeral.

\d matches 1 numeral (so you'll have at least one number somewhere)

You need a starting alphabet, and at least one numeral. That leaves at most 6 more characters, hence the {0,6} part.

So, the first part, the lookahead fulfills your length requirement (between 6 and 8 characters). The second part fulfills your "start with alpha, and at least 1 numeral" requirement. Combine them and you have done it!

For more information, check this regex lookahead article.

FYI, \w matches alphabets, numerals and the underscore character, so it might not be what you want.

Hope this helps!
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:55PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month