Use Regular Expression to Allow “Numbers, Letters and 3 Spaces” in Username Field?

You could use: if(preg_match('@^(\w*\s){0,3}\w*$@', $_POST'txt_username)) { // do something } See it in action on: rubular.com Note: \w includes the underscore (_). If you don't want it, you can use: if(preg_match('@^(^\W_*\s){0,3}^\W_*$@', $_POST'txt_username)) { // do something } Instead. EDIT: Since the OP decided to accept my answer, I added Justin's improvements.

1 Good answer but I would specify that this includes an extra character (_), just in case. – NickC Aug 25 '11 at 4:16 The superior answer. – cwallenpoole Aug 25 '11 at 4:19 1 This will lead to catastrophic backtracking if the input has less than 3 spaces.

The engine will not be able to decide where one (^\W_\s? ) group ends and another begins, so it will keep trying different permutations until it uses up a ton of system resources. A reasonably long input (not very long at all) will cause the system to hang.

– Justin Morgan Aug 25 '11 at 4:29 1 It is just plain silly and complicated and obfuscatory — and wrong, actually — to write ^\W_ when what you mean is simply \pL\pN, although you really ought to through in \pM there too for reasons that you will hate. The 7 Unicode general category charclasses — \pL, \pN, \pM, \pS, \pP, \pZ, and \pC, plus their \P inverses — should be in every regex programmer’s arsenal. They are what you should turn to whenever you want specific types of character.

And they are only one character longer than \w and such, which although should work, due to PHP being stupid, don’t. – tchrist Aug 25 '11 at 5:12 2 Moreover, the 7 charclasses work whether you have ASCII or Unicode, whereas due to PHP and PCRE conspiring to break Unicode regexes, the things like \w and \s and \b will not. Which do you prefer: something that always works no matter the data, or something that breaks or even worse has security holes depending on what data you feed it?

Until they fix how the PCRE that PHP is linked against is compiled and optioned, you have to use the 7 classes, not the shortcuts, if you care about reliability, and write once and run anywhere. Otherwise, you lose. Make the 7 charclasses automatic grabs.

– tchrist Aug 25 '11 at 5:17.

(^\W_*\s){0,3}^\W_*$/ No catastrophic backtracking, since the (^\W_*\s) groups are clearly delimited. Edit: Adopting tchrist's unicode-friendly version: /^(\pN\pL\pM*\s){0,3}\pN\pL\pM*$.

Aug 25 '11 at 4:41 Alphanumerics are \pN\pL\pM; non-alphanumerics are ^\pN\pL\pM. – tchrist Aug 25 '11 at 5:00 @Null - php.net/manual/en/regexp.reference.unicode.Php – Kobi Aug 25 '11 at 5:08 @Kobi Yeah, saw that. Still wondered what "Mark" was, but I found it here: regular-expressions.

Info/unicode. Html – NullUserException? Aug 25 '11 at 5:09 @tchrist - Thanks, edited to include your version.

I've read some of your work elsewhere on Unicode handling, good to hear from you on this. – Justin Morgan Aug 25 '11 at 5:42.

If you don't want to consecutive spaces, and no spaces near the edges, you can try: preg_match("#^\w+(?: \w+){0,3}$#", "123 4 5", $matches); if($matches) print_r(":-)"); If you don't care about consecutive spaces, a similar option is ^\w*(?: \w*){0,3}$ Or, a more modern approach, with a lookahead (which is good for adding more constrains): ^(?! ^ *(?: ^ *){4})\w *$ Either way, note that \w includes underscores, you may want to replace it with something more suitable, for example a-zA-Z\d or the Unicode aware \p{L}\p{N}.

Hmm... Haven't been downvoted in a while now. Any comment on why? I'm always happy to improve my answers.

– Kobi Aug 25 '11 at 4:58 1 In a Unicode regex engine, \w is \p{Alphabetic=True}\p{GC=Decimal_Number}\p{GC=Letter_Number}\p{GC=Mark}\p{GC=C? Onnector_Punctuation}. However, getting PHP to work that way is a real pain, because you have to build PCRE the right way, and nobody ever does.

Probably the best you can do is \pL\pM\p{Nd}\p{Nl}\p{Pc} — even though you should never havce to write all that just to mean \w. – tchrist Aug 25 '11 at 5:04 Well, I think I was pretty close :). You got me on spaces though.

Thanks! – Kobi Aug 25 '11 at 5:10 @tchrist A lil' grumpy today, huh? – NullUserException?

Aug 25 '11 at 5:10 @Null - Try this one, it's an excellent read: Why does modern Perl avoid UTF-8 by default? – Kobi Aug 25 '11 at 5:11.

I would do something like this: if( count(explode(' ', $_POST'txt_username')) "valid":"invalid"); } /* hello world is valid hello wo rl d is valid h e l l o w o r l d is invalid hello123@@ is invalid */ see it here on tehplayground.com.

NullUserException, thanks for catching that. Updated. – macek Aug 25 '11 at 4:22.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions