Click here to Skip to main content
12,699,123 members (25,198 online)
Rate this:
Please Sign up or sign in to vote.
I am creating a username validation. I am using this regex:

which takes input that allows lower case, upper case, numbers, hyphens, and underscores. The hyphen and underscore must not be at the start or end of the string. I do not want the username to include two consecutive hyphens or underscores, like Aquib- -qureshi or aquib_ _qureshi (spaces shown for clarity only).

Can I do this using regex or do I have to write a piece of code to look for those consecutive characters?

How to do that? Can anyone help me out with an example?

Thanks in advance.
Posted 30-Sep-12 8:32am
Updated 30-Sep-12 9:26am
ridoy 30-Sep-12 14:21pm
no you don't need to use code for that..add _ _/-- with your regular expression

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1



to limit to 6 characters like your example.

The magic sauce is the negative lookaround assertion.

Please mark answered if I helped you. —Yvan
ridoy 30-Sep-12 14:22pm
yes that's the right one as i said...+5
qureshiaquib 30-Sep-12 14:30pm
this will take 5 charecters right? So how 6 will be taken? I didint got you
Yvan Rodrigues 30-Sep-12 14:32pm
No, it will take between zero and four character matches; plus the start and end characters == 6.
qureshiaquib 30-Sep-12 14:37pm
what diffrence between your regex and this one?
Can you explain bro
Yvan Rodrigues 30-Sep-12 14:46pm
That one means:
- start with between 1 and 5 characters from the set a-z, A-Z, 0-9.
- the next character MUST be a hyphen or an underscore.
- end with between 1 and 5 characters from the set a-z, A-Z, 0-9.

- there must be between 3 and 11 characters.

Do yourself a favour and download Expresso and read the "30 Minute Regex Tutorial" on this site.
qureshiaquib 30-Sep-12 14:57pm
sorry bro. But i have download its not easy to understand. I cant get there anything so i asked here.
And by the way.
what diffrence between your regex and this one?
Can you explain bro
is correct? And same as your right?
Your output and this output will be equal
Yvan Rodrigues 30-Sep-12 15:36pm
No, not the same.
First, yours can be shortened to ^[a-zA-Z0-9]{1,5}([\-_][a-zA-Z0-9]{1,5})?$; however it is more restrictive than mine.
Here are some examples of differences.
Mine will allow, yours will disallow:

qureshiaquib 30-Sep-12 15:48pm
but in any form have you seen any username which will have underscores and hifen in a single at the same time?
Yvan Rodrigues 30-Sep-12 15:52pm
I don't follow. I think you need to clarify exactly what you wish to allow/disallow and what your allowable minimum/maximum lengths are.
qureshiaquib 30-Sep-12 15:53pm
like sally-jean_smith?
It is valid?? For username?
And in your case
only 7 is accpetable? Is is not a username kind thing. Do you think
qureshiaquib 30-Sep-12 16:04pm
i need i username
which will be minimum of 4 charecters and maximum of 15 chracters
which will allow hifen and underscore and dot in the middle but not in starting and ending of username
(which will not allow symbols except hifen and underscore and dot)
should disallow
aquib_ // means no symbols will be there at end
and username should not be numbers it should be mixing of numbers and alphabets or it should be only alphabets
hope this will be understoo
Yvan Rodrigues 30-Sep-12 16:45pm
Can certainly be done with regex. Due to all the clauses it will require at least 3 expressions separated by the | operator. Unfortunately you'll need to find someone with more spare time than me.
qureshiaquib 7-Oct-12 1:12am
bro i'v got this regex
which is same as i want it doesnt take
means it doesnt take any symbol at starting and ending and cannot take any consecutive symbols
but the problem is it takes this type of values
i want only one symbol in a row.
can you help me please
qureshiaquib 30-Sep-12 16:53pm
okay i though you will help me. I thought i should add dot with dash and underscore in my regex. But my bad luck. That your not helping.
But Any ways have nice time bro

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 30 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100