James Moore
James Moore

Reputation: 3617

preg_match problem

I have written this function:

public static function isDecimal($value, $places = 2)
    {
        if(preg_match("/^[0-9]+(\.[0-9]{".$places."})?$/", $value)) {
            return true;
        } else {
            return false;
        }
    }

Which is designed to test whether the value being entered is a number/decimal number. However, if I enter the number "23c.32" or "2b3.23" it returns it as true. Can anybody point out where the fault lies in my regex?

Upvotes: 1

Views: 221

Answers (3)

Drazisil
Drazisil

Reputation: 3343

You may want to check out is_numeric() and is_float().

Built in functions that appear to do what you are looking for.

Upvotes: 1

Pianosaurus
Pianosaurus

Reputation: 5758

It works just fine here. The problem must be somewhere near the function call.

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

That regex is fine, it returns false if you use it with the examples you provided.

Upvotes: 1

Related Questions