I am trying to find a piece of regex to match a currency value.
I would like to match only numbers and 1 decimal point ie
Allowed
Not Allowed
I have search and tried quite a few without any luck.
Hope you can advise
if (preg_match('/^[0-9]+(?:\.[0-9]+)?$/', $subject))
{
# Successful match
}
else
{
# Match attempt failed
}
Side note : If you want to restrict how many decimal places you want, you can do something like this :
/^[0-9]+(?:\.[0-9]{1,3})?$/im
So
100.000
will match, whereas
100.0001
wont.
If you need any further help, post a comment.
PS If you can, use the number formatter posted above. Native functions are always better (and faster), otherwise this solution will serve you well.
How about this
if (preg_match('/^\d+(\.\d{2})?$/', $subject))
{
// correct currency format
} else {
//invalid currency format
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With