0

I would like to create a preg_match function to validate my username, my code below not working perfectly, especially on Must contain at least 4 letter lowercase rules and number not more than 4 character and place behind letter

if (preg_match('/^[a-z0-9]{4,12}/', $_POST['username']))

Here are my username rules that I want to work :

  • Only contain letter and numbers but number is not reqired
  • Must contain at least 4 letter lowercase
  • Number not more than 4 character and place behind letter
  • Must be 4-12 characters

Thank you for any help you can offer.

chris85
  • 23,846
  • 7
  • 34
  • 51
daniel
  • 31
  • 1
  • 1
  • 6
  • 1
    can sites stop forcing me to abide by arbitrary rules for my user-name please. –  Feb 05 '18 at 01:15
  • Tell us; what isn't working the way you'd like it to? What results are you presently getting, as opposed to the desired results? You didn't describe the problem. – Funk Forty Niner Feb 05 '18 at 01:18
  • 1
    we don't read minds, please remember that in case you're still here. I myself have left since it's unclear what it is you're asking. – Funk Forty Niner Feb 05 '18 at 01:26
  • Take a look at https://stackoverflow.com/a/19605207/4333555, it's not exactly what your asking but it can lead you there. Also don't forget closing anchor. – chris85 Feb 05 '18 at 01:28
  • @chris hey stranger, long time no see. Hope all is well your way, *cheers* (I only revisited this post to see if there was any update/comments left). Alas, no. – Funk Forty Niner Feb 05 '18 at 02:10
  • @FunkFortyNiner Ah, helllo Fred. Yea, kinda getting busy with side projects, havent been around much. – chris85 Feb 05 '18 at 19:02
  • @chris85 Kind of the same here; got me a fine issue with a server moving to a new location and has been a sort of nightmare but I am managing. There's always more than one way to skin a cat, as it were ;-) I didn't think you'd know who I was, given I changed my profile name; it's more fitting for me *lol!* cheers man, take care. – Funk Forty Niner Feb 05 '18 at 19:03

2 Answers2

1

You match these criteria, maybe this will be an option:

^[a-z](?=(?:[a-z]*\d){0,4}(?![a-z]*\d))(?=[a-z\d]{3,11}$)[a-z\d]+$

This will match

  • From the beginning of the string ^
  • Match a lowercase character [a-z]
  • A positive lookahead (?= which asserts that what follows is
    • A non capturing group (?:
    • Which will match a lowercase character zero or more times followed by a digit [a-z]*\d
    • Close non capturing group and repeat that from 0 to 4 times ){0,4}
    • A negative lookahead (?! Which asserts that what follows is not
    • A lowercase character zero or more times followed by a digit [a-z\d]*
    • Close negative lookahead )
  • Close positive lookahead )
  • Positive lookahead (?= which asserts that what follows is
    • Match a lowercase character or a digit from 3 till 11 times till the end of the string (?=[a-z\d]{3,11}$)
  • Close positive lookahead )
  • Match a lowercase character or a digit till the end of the string [a-z\d]+$

Out php example

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

Regex: ^[a-z]{4,8}[0-9]{0,4}$|^[a-z]{4,12}$

Details:

  • ^ Asserts position at start of a line
  • $ Asserts position at the end of a line
  • [] Match a single character present in the list
  • {n,m} Matches between n and m times
  • | Or

PHP code:

$strings=['testtesttest', 'testtesttestr', 'test12345', 'testtest1234', 'testte123432'];

foreach($strings as $string){
    $match = preg_match('~^[a-z]{4,8}[0-9]{0,4}$|^[a-z]{4,12}$~', $string);
    echo ($string . ' => len: (' . strlen($string) . ') ' .($match ? 'true' : 'false')."\n");
}

Output:

testtesttest => len: (12) true
testtesttestr => len: (13) false
test12345 => len: (9) false
testtest1234 => len: (12) true
testte123432 => len: (12) false

Code demo

Srdjan M.
  • 3,310
  • 3
  • 13
  • 34