Alexander Lomia Alexander Lomia - 3 months ago 16
PHP Question

Regex for validating alpha_dash and exactly one dot symbol

I want to validate names of uploaded files. For now, I have the following regex which matches only alphanumeric characters, dashes and underscores:

/^[\w-]*$/


However, I need to also ensure, that the filename contains exactly one dot character. How should I modify this regex to achieve the desired result?

Answer

Pull the period out of your character class, like this:

/^[\w-]*\.[\w-]*$/
Comments