Search code examples
regexpreg-match

RegEx fails on checking maximum length


$regex = '/^(.+){,16}$/'

Nothing is matching, everything fails!


Solution

  • There are 2 problems:

    1. {,16} matches the characters {,16} literally, you should use {0,16}
    2. You have 2 quantifiers so instead of .+{,16} you should use .{0,16}

    Regex that should work for you:

    $regex = '/^.{0,16}$/'; // will match empty input also
    

    RegEx Demo