Forum Moderators: coopster
I found a regEx online for matching a username, allowing spaces and such. But the regEx pattern has no limits on the character length of the string it should match. I want usernames to be between 3 and 15 characters. How can I modify this regular expression to apply this character length limitation on the username?
^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$
Thanks for any help and guidance!
Kev
^(?=.{3})(?!.{13})[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$
(?=.{3}) asserts that the string has at least 3 characters
(?!.{13}) asserts that the string doesn't have 13 characters or more (12+1)
Nice puzzle. :)
BTW, regex assertions can consume a lot of cpu, i think. You're much better off checking the length of the username with strlen() outside of the regular expression, but i assume you have your reasons for doing it all in one statement.