FlyingUnderpants FlyingUnderpants - 13 days ago 5
PHP Question

preg_match check for number and whitespaces

I'm working on a simple login & register system as practice and I want to check strings if they contain a number and at te same time with preg_match, but how can I make that happen?

if (preg_match('~[0-9]~', $name)) { echo "match";}

This is the code I have so far. It only can find a number inside a string, but how can I add that the preg_match also searches for whitespaces. I have tried multiple things but I just can't find a way to make it work. I hope someone can help me out.

If there is something unclear please let me know, and I will do my best to help you.

Thanks in advance :D

Dez Dez

If you want to find white spaces you have to use \s in the regular expression.

preg_match('~[0-9\s]~', $name)