How do I validate a string to make sure it only contains numerals and allow spaces?
I want to make sure that there are numbers entered, and if so, spaces may be used.
I have used is_numeric but this does not allow spaces.
Thanks
example:
$str='1d23123 123213'; if (is_numeric(str_replace(' ','',$str))) echo 'ok'; else echo 'false';
Can also just use a regular expression.
if(ereg('[^0-9 ]', $string)) echo 'Invalid!'; else echo 'Valid!';
Don't even need regular expressions.
if(strspn($string,'0123456789 ')==strlen($string)) //valid
if(ltrim($string, '0..9 ')=='') //valid
Cuddle up with the manual, it's good for a giggle.