I need a php regex for a numeric value, including "-" and ",",

In php, is_numeric function does not allow '-' and ','.

-1
source share
3 answers

This should work:

preg_match("#^-?\d+(,\d+)?$#", "-1,2", $match);

Matching one or more digits:

"#\d+#"

Optionally matches a comma followed by one or more digits:

"#\d+(,\d+)?#

It is not necessary to match the "-" sign:

"#-?\d+(,\d+)?#"

Allow only this and nothing:

"#^-?\d+(,\d+)?$#"
+5
source

is_numeric()gives negative numbers. I think the problem is only a comma.

is_numeric( str_replace( ',', '.', $number ) );

See also Converting a decimal point to a floating point.

+2
source

^[0-9 ,-]+$
0

All Articles