View Full Version : PHP Preg Match
DanielJackins
March 1st, 2008, 04:48 PM
Hi, I'm currently trying to create a registration script, and its all going well except that you can enter invalid values in all of the fields. I can't seem to find a tutorial on preg match, or anything else explaining it very well. I need usernames to be restricted to a-z, A-Z, 0-9, and underscores, and 5 characters minimum, same with passwords. I also need it so they must enter a valid email address (eg goof@goof.net, cause right now they can enter anything, such as just goof)
Thanks
jacobmp92
March 1st, 2008, 05:02 PM
I've never really found a really good tutorial all in one; I've learned by examples. So I'll show you some PCREs for what you might be looking for, like a username::
/^\w{5,}$/
The /'s at the beginning and end are the boundaries of the actual expression. The ^ matches from the beginning, and the $ does from the end. If you leave these out, it might only match a part of the input.
The \w is equivalent to [a-zA-Z0-9_], and the {5,} modifies the \w to require at least 5 characters. To add a limit on characters, just add another value to the right of the comma. For example, {5,20} means it must be between 5 and 20 characters.
Emails get more complicated; there are a lot of email regexes out there and everyone seems to like a different one. A quick google search for "email regular expression" should bring up some decent results.
DanielJackins
March 2nd, 2008, 05:48 AM
Thanks, that cleared up what all that means, but what else would I put?
vBulletin® v3.7.4, Copyright ©2000-2008, Jelsoft Enterprises Ltd.