Search code examples
phpregexvalidationnumbersfilenames

Validate a numeric filename which may only be between 01 and 12, then the file extension


I am trying to accept filenames from 01.jpg to 12.jpg with this regexp:

preg_match(
    '/^([0-1][0-2]\.jpe?g)$/i',
    $_FILES['Filedata']['name']
)

My attempt works fine when validating 01.jpg, 10.jpg, 11.jpg, and 12.jpg, but it fails to properly match filenames 02.jpg through 09.jpg.


Solution

  • try this

     '/^(0[1-9]|1[0-2])\.jpe?g$/i'