Click here to Skip to main content
12,252,552 members (62,698 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 30-Sep-12 20:13pm
sariqkhan1.4K
Edited 30-Sep-12 20:39pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
sariqkhan 1-Oct-12 3:25am
   
thanks i was not knowing
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
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 R. 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 R. 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
sariqkhan 1-Oct-12 7:46am
   
i have gone through your regex bro it is taking only numreicsc also
like this 9998897895654
can you remove this fault of numerics?
please bro i am in need
sariqkhan 1-Oct-12 4:29am
   
ohkay. Thanx and can you do me a favour
can you provide me with simple regex
which will take only alphabets and numbers
mininum characters of 3 and maximum of 15
which will not accept white spaces in between
and it should not contain only numbers. Username could not be numbers so mixing of alfanumeric or alphabets. But not only numerics
Manfred R. Bihy 1-Oct-12 4:32am
   
Sure, the following will do that for you:

^[a-zA-Z0-9]{3,15}$

Cheers!
sariqkhan 1-Oct-12 4:37am
   
: D
thanx but it will also take only numerics? So what to do? I have to write a piece of code for that? ?
Manfred R. Bihy 1-Oct-12 6:09am
   
You could say for instance:
^[a-zA-Z][a-zA-Z0-9]{2,14}$
but that would mean the user name must start with alpha only nd could then possibly be followed by only digits.
Essentially I'd say your scenario is best solved by writing you own validation. Regular expressions don't have the capabilities needed in you special case. In my solution for instance it is impossible for the trailing sub expression to know how many characters were matched in the previous expression, but this would be needed to assign the correct values to the quantifier expression {n, m}. I'd still go with what I advised before: Check if the string has the correct length in your code and if the length is within the specified limits then use the RegEx to do the rest.
sariqkhan 1-Oct-12 6:39am
   
ohkay. i got you bro
thanks for helping me
: )
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
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
qureshiaquib 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2016
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