16
Oct
Learn how to use PHP regex to validate float numbers
If you want only input 0-9 and only once “.” then you can use following regex.
/^-?(?:\d+|\d*\.\d+)$/
This matches normal floats e.g. 3.14, shorthands for decimal part only e.g. .5 and integers e.g. 9 as well as negative numbers.
echo preg_match('/^-?(?:\d+|\d*\.\d+)$/', '123.12'); // return 1 echo preg_match('/^-?(?:\d+|\d*\.\d+)$/', '123.12.2'); // return 0