[RESOLVED] Regular expressions - Username
I already have a function to check for a valid e-mail address using regular expressions, but I had written it awhile ago when I was still familiar with them.
Now I need one to check for a valid username. The username can only have letters, numbers, underscores (_), spaces, and a single period.
It has to start with a letter. It cannot have consecutive spaces (2 or more in a row).
Can someone help me? :(
Re: Regular expressions - Username
Something like
[a-zA-Z]([a-zA-Z0-9_]|( [a-zA-Z0-9_]))*(\.([a-zA-Z0-9_]|( [a-zA-Z0-9_]))*)?
In words: with a name character being an alphanumeric character or an underscore, a username is
an alphabetic character followed by
either a name character, or a space and a name character, repeated any number of times
followed optionally by
a dot and
either a name character, or a space and a name character, repeated any number of times
If you don't want to allow the dot as the last character, replace the last * by a +.
If you don't want to allow the dot as the last character and don't want a space to follow the dot, insert a name character collection after the dot, unconditionally.
As it is, the space character cannot be the last character, nor can it be directly before the dot.
Re: Regular expressions - Username
I hope you realise that properly validating email addresses using regular expressions is fairly abstruse. Simple validation is fine, but you invariably cut out 1% of potential addresses.
Re: Regular expressions - Username
Quote:
Originally Posted by CornedBee
Something like
[a-zA-Z]([a-zA-Z0-9_]|( [a-zA-Z0-9_]))*(\.([a-zA-Z0-9_]|( [a-zA-Z0-9_]))*)?
In words: with a name character being an alphanumeric character or an underscore, a username is
an alphabetic character followed by
either a name character, or a space and a name character, repeated any number of times
followed optionally by
a dot and
either a name character, or a space and a name character, repeated any number of times
If you don't want to allow the dot as the last character, replace the last * by a +.
If you don't want to allow the dot as the last character and don't want a space to follow the dot, insert a name character collection after the dot, unconditionally.
As it is, the space character cannot be the last character, nor can it be directly before the dot.
Wow thanks, that looks promising. I'll give it a try and report back. :)
Quote:
Originally Posted by penagate
I hope you realise that properly validating email addresses using regular expressions is
fairly abstruse. Simple validation is fine, but you invariably cut out 1% of potential addresses.
Wow. I'm not sure how well that would perform in PHP. Especially under high traffic. Shouldn't be too much of a problem, though. If they really want to register they'll use a different e-mail address. Thanks.
Re: [RESOLVED] Regular expressions - Username
cornedbee: wow this is good. can you pls modify it to not include white space characters? thanks, greatly appreciate it
or is this correct
[a-zA-Z]([a-zA-Z0-9_]|([a-zA-Z0-9_]))*(\\.([a-zA-Z0-9_]|([a-zA-Z0-9_]))*)?