PHP regex - valid float number
Solution 1:
this is what you're looking for
$re = "~ #delimiter
^ # start of input
-? # minus, optional
[0-9]+ # at least one digit
( # begin group
\. # a dot
[0-9]+ # at least one digit
) # end of group
? # group is optional
$ # end of input
~xD";
this only accepts "123" or "123.456", not ".123" or "14e+15". If you need these forms as well, try is_numeric
Solution 2:
/^-?(?:\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.