Forum Moderators: coopster

Message Too Old, No Replies

Quick Modification of Username RegEx

Character length limits?

         

curlybill09

10:35 pm on Sep 16, 2009 (gmt 0)

10+ Year Member



Hi guys,

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

Kings on steeds

9:44 am on Sep 17, 2009 (gmt 0)

10+ Year Member



simple validation on usernames would be something like


^[A-Za-z0-9_]+{3,12}*$

will limit it to letters, numbers, underscores and hyphens must be between 3 and 12 chars long

curlybill09

10:56 am on Sep 17, 2009 (gmt 0)

10+ Year Member



Thanks, but I need to apply the character limit to the pattern I posted above. I know how to do it for the pattern you supplied... I'm asking how to do it with the pattern I posted.

idfer

3:47 pm on Sep 17, 2009 (gmt 0)

10+ Year Member



Here you go:

^(?=.{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.