Click here to Skip to main content
15,923,374 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
My friend is making windows form and he needs to vlidate a username which will be a minimum of 4 characters and maximum of 15 characters long. It will also allow hyphens and underscores as well as dots in the middle, but not at the start and neither at the end of the username. There may be no more than one hyphen, one underscore and one dot in a row.
Examples of disallowed usernames:
-aquib
_aquib
.aquibxyz
aquib.
aquibxyz--qureshi
aquib__xyzqureshi
aquibqureshi-
aquib..qureshi
aquib_     // means no symbols will be there at end

The username should not be only digits it should be either a mix of digits and alphabetical characters or it should be only alphabetic.
I hope this will be understood
I have got this regex:
^([a-zA-Z0-9](?(?!__|--)[a-zA-Z0-9_\-]){0,4}[a-zA-Z0-9])$

but it is not too usefull in his case.
Can any one help me out?

Thanks in advance!
P.S.: I have downloaded expresso but I am not sufficient in that environmen. It's way too difficult to undestand for me.
Posted
Updated 30-Sep-12 20:39pm
v2

There is a dedicated regex forum[^]. The people there will more likely be able to help you than those in the multi-purpose Q&A.
 
Share this answer
 
Comments
sariqkhan 1-Oct-12 3:25am    
thanks i was not knowing
What you want would be hard, if not impossible, to do with regular expressions, but there is one way you could pull this through if you can agree to separate the length check from the regular expression stuff. First check would be if the length of the username is within the specified bounds and if it is check the username with this here regex:

^(?:[a-zA-Z0-9](?:\.|_|-)?)+[a-zA-Z0-9]$


Regards,

— Manfred
Modified: I had to change the capturing parenthesis () to non capturing (?: ... ) and now it works like a charm.
 
Share this answer
 
v3
Comments
sariqkhan 1-Oct-12 3:16am    
your regex allows
aquib_qureshi-qureshi
which is not suitable
and your regex is taking unlimited length of characters
and also it takes only numbers a username should not be only numbers?
it should be alphabets or mixing of alphabets and numbets and hifens and dash
can you help please
Manfred Rudolf Bihy 1-Oct-12 3:27am    
With your specification it is impossible to also get the length limits coverd with a regular expression. That is what I stated in my solution and said that you'd have to check the length separately in your code before doing the validation with a regular expression.
sariqkhan 1-Oct-12 3:30am    
okay. i got you.
but can you provide me the simple regex which i stated?
means all the condition which i stated
i thing in my prevoius comment i have stated that
aquib_qureshi-qureshi
Manfred Rudolf Bihy 1-Oct-12 3:38am    
Now I don't understand you! "but can you provide me the simple regex which I stated" what is that supposed to mean.
My regex works, but the length checks are impossible to cover in this scenario. There's nothing more to be said.
sariqkhan 1-Oct-12 7:13am    
awsome bro
^(?:[a-zA-Z0-9](?:\.|_|-)?)+[a-zA-Z0-9]$
this one is really awsome
thanks for your help
buddy
OK, I've now worked out how to do it almost perfectly. For explanation purposes I've split the expression into three lines, but if you will use it it will have to written in one line like below:

(?!^[a-zA-Z0-9\.\-_]{1,2}$)(?!^[a-zA-Z0-9\.\-_]{16,}$)(?!^[0-9]{3,15}$)^(?:[a-zA-Z0-9](?:\.|_|-)?)+[a-zA-Z0-9]$


Explanation:
There is a grouping operator for regular expressions that is called zero-width negative lookahead (?! < subexpression >). What does that mean? Let's dissect this monster description:

  1. Zero-width lookahead
    This essentially means that this matching expression will not consume any characters. So when this expression has matched any following non zero-width expression will start of at the same place in the string as the zero-width regular expression did.
  2. Negative
    This signifies that all following expressions can only match, if this expression did not previously produce any matches.


(?!^[a-zA-Z0-9\.\-_]{1,2}$) // This expression will assert that a username with characters a-z A-Z 0-9 . _ and - is no less than three characters long
(?!^[a-zA-Z0-9\.\-_]{16,}$) // This expression will assert that a username with characters a-z A-Z 0-9 . _ and - is no longer than 15 characters
(?!^[0-9]{3,15}$)           // This expression will assert that a username will not be made up of only digits [0-9]
^(?:[a-zA-Z0-9](?:\.|_|-)?)+[a-zA-Z0-9]$

Given the three assertions at the beginning of the regular expression we can now be sure that there are at least 3 characters and no more than 15 and that a username of only digits is also disallowed. The ^ and $ in the assertions are important here.

Now lets have a look at the final part which is also broken into several lines to better annotate it:

^                // Start match at the beginning to the string
(?:[a-zA-Z0-9]   // Make sure that a username will not start with a '.' a '-' or a '_', but only alphanumeric characters
    (?:\.|_|-)?  // After an initial alpha numeric a '.' a '-' or a '_' may follow, but not two in a row ...
)+               // ... so we repeat this pattern once or many times (+). Drawback: ._ .- _- etc. are also not allowed, which I think we can live with here.
[a-zA-Z0-9]      // Finally we make sure that there is an alphanumeric character in the last position ...
$                // ... exactly at the end of the string.


I do hope you could follow my explanations and found this solution helpful. The only drop of bitternes to me is that we now have an additional constraint:
While it is true that the each of the characters '.', '-' and '_' may appear no more than once in row (valid constraint from OP), but now none of the characters in the class [\.\-_] can appear one after another (new constraint introduced through solution).

Regards,

— Manfred
 
Share this answer
 
Comments
sariqkhan 2-Oct-12 9:04am    
yup bro.
you have really worked hard to achieve this
the only problem is it is taking
aquib_q-q.qures
and
aquib_qureshi_manfred
but it is really good.
nice shot bro
thanks
can you modify it further? if you can it will be more better
and a big shot in regex history.
because no one have made such a complex. i have searched and asked to many developer
thanks to you
shaikh-adil 2-Oct-12 11:59am    
+5

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900