php only allow letters, numbers, spaces and specific symbols using pregmatch

CudoX picture CudoX · Jun 13, 2013 · Viewed 76k times · Source

on my php i use preg_match to validate input texts.

if(preg_match('/^[a-zA-Z0-9]+$/', $firstname)) {
}

But this only allows alphanumeric and does not allow spaces. I want to allow spaces, alpha and numeric. and period(.) and dash(-)

Please help me out here? thanks in advance.

Answer

Baba picture Baba · Jun 13, 2013

Use

preg_match('/^[a-z0-9 .\-]+$/i', $firstname)