Simple Username Creation Validation with PHP
When I create login areas (mostly intranets) for small websites, I'm always asked by the customer to keep
usernames to letters and numbers. That means no email addresses as usernames
and special characters like "_", "-", and ".". This, in my customer's mind,
keeps the login easy for their users and limits the number of support calls
they will receive. While I don't recommend disallowing common username
characters like the ones cited above, I do understand their need for simplicity.
Here's how, using PHP, I validate that a username is only letters and numbers.
The PHP
function validate_username($input,$pattern = '[^A-Za-z0-9]')
{
return !ereg($pattern,$input);
}
It's as easy as that. I don't go as far as using this for passwords, but you could if you wanted to. I'll also mention that if I want to allow non-alphanumeric characters, I just need to change the function's pattern. Easy enough!
![I’m an Impostor]()
This is the hardest thing I've ever had to write, much less admit to myself. I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life. All of those feelings were very...
![An Interview with Eric Meyer]()
Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it?
At first blush, it was the simplicity of it as compared to the table-and-spacer...
![Elegant Overflow with CSS Ellipsis]()
Overflow with text is always a big issue, especially in a programmatic environment. There's always only so much space but variable content to add into that space. I was recently working on a table for displaying user information and noticed that longer strings were...
![Google Extension Effect with CSS or jQuery or MooTools JavaScript]()
Both of the two great browser vendors, Google and Mozilla, have Extensions pages that utilize simple but classy animation effects to enhance the page. One of the extensions used by Google is a basic margin-top animation to switch between two panes: a graphic pane...
I like the approach — instead of matching a word boundary e.g. ^[a-zA-z0-9]*$ you match the first invalid character. I wonder what the speed difference is…
BTW, you can use [^\w\d] insead ;) It’s lazier
My bad. You cant use \w since it matches _ as well. On the other hand \w handles \d, so if you are looking for something quick for chars, digits and _ — you can use \w.
You could also use PHP’s ctype_alnum to match only alphanumeric characters
which matches (ever so slightly) faster than its equivalent regular expression,
and still retain future flexibility by using something like:
Of course the speed difference is on the order of nanoseconds,
but it is faster nonetheless.
A better method name would be useful. ;)
@Jeff: Ooops. I have this function within a class. I actually use it like this:
I simply didn’t rename it for the blog. I’ll do that quick!
A word of warning,
ereg
is being removed in PHP6 because of the more powerful and usually fasterpreg
. Altering your code to work withpregs
is easy and safer should the server you’re on upgrade to PHP6 sometime in the future:ereg will be deprecated in php 5.3 and removed in 6, use preg instead
ereg is not available in php 5.3.0
any alternative for this ?
I’m rather new to regexp, and am just trying to get something exactly like this to work, but it just isn’t. I got annoyed, and then simplified as much as possible to this:
and it isn’t responding anything except 0, no matter what I try.
Oops sorry. Anyways, it’s just a text input that posts to itself, and it deals with the post by doing
The Best way I recommend is this :-