Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I am creating a username validation. I am using this regex:
^[a-zA-Z0-9][a-zA-Z0-9_\-]{0,4}[a-zA-Z0-9]$
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 7:32am
Edited 30-Sep-12 8:26am
v2
Comments
ridoy at 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
good
Please Sign up or sign in to vote.

Solution 1

^([a-zA-Z0-9](?(?!__|--)[a-zA-Z0-9_\-])+[a-zA-Z0-9])$
or
^([a-zA-Z0-9](?(?!__|--)[a-zA-Z0-9_\-]){0,4}[a-zA-Z0-9])$
to limit to 6 characters like your example.
 
The magic sauce is the negative lookaround assertion.
 
Please mark answered if I helped you. —Yvan
  Permalink  
v3
Comments
ridoy at 30-Sep-12 14:22pm
   
yes that's the right one as i said...+5
qureshiaquib at 30-Sep-12 14:30pm
   
{0,4}
this will take 5 charecters right? So how 6 will be taken? I didint got you
Yvan Rodrigues at 30-Sep-12 14:32pm
   
No, it will take between zero and four character matches; plus the start and end characters == 6.
qureshiaquib at 30-Sep-12 14:37pm
   
what diffrence between your regex and this one?
Can you explain bro
^[a-zA-Z0-9]{1,5}(-[a-zA-Z0-9]{1,5}|_[a-zA-Z0-9]{1,5})?$
Yvan Rodrigues at 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 at 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
^[a-zA-Z0-9]{1,5}(-[a-zA-Z0-9]{1,5}|_[a-zA-Z0-9]{1,5})?$
is correct? And same as your right?
Your output and this output will be equal
Yvan Rodrigues at 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:
 
happy_fun_user
rabbit-foot
7
sally-jean_smith
qureshiaquib at 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 at 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 at 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 at 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
_aquib
.aquibxyz
aquib.
aquibxyz--qureshi
aquib__xyzqureshi
aquibqureshi-
aquib..qureshi
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 at 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 at 7-Oct-12 1:12am
   
bro i'v got this regex
(?!^[a-zA-Z0-9\.\-_]{1,2}$)(?!^[a-zA-Z0-9\.\-_]{16,}$)(?!^[0-9]{3,15}$)^(?:[a-zA-Z0-9](?:\.|_|-)?)+[a-zA-Z0-9]$
which is same as i want it doesnt take
aquib__qureshi
aquib..qureshi
aquib--qureshi
-aquib
aquib-
aquib_
aquib_
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
aquib_qureshi-
i want only one symbol in a row.
can you help me please
qureshiaquib at 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
0 Sergey Alexandrovich Kryukov 430
1 Gihan Liyanage 347
2 ClimerChinna 222
3 vikinghunter 176
4 OriginalGriff 151
0 Sergey Alexandrovich Kryukov 8,373
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,025
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 30 Sep 2012
Copyright © CodeProject, 1999-2014
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