How to check if an entered value is currency. Preferably by regular expression or php function.
(values like 1550.50
, 1500
or 100.75
)
I guess what you really want is a way to tell any number from a number that makes sense as a currency value. So 1.04e-7
probably shouldn't match, and neither should 1.234
or 12.3
, even though all are of course numeric.
Finally, you'd have to expect thousands separators like 1,234.56
(which, like the decimal point, might also vary between locales). So, assuming that you only ever want to check currency values using the dot as decimal separator and the comma as an optional thousands separator, try this:
/\b\d{1,3}(?:,?\d{3})*(?:\.\d{2})?\b/
Explanation:
\b # word boundary assertion
\d{1,3} # 1-3 digits
(?: # followed by this group...
,? # an optional comma
\d{3} # exactly three digits
)* # ...any number of times
(?: # followed by this group...
\. # a literal dot
\d{2} # exactly two digits
)? # ...zero or one times
\b # word boundary assertion
I use this:
function isCurrency($number)
{
return preg_match("/^-?[0-9]+(?:\.[0-9]{1,2})?$/", $number);
}
//usage examples
echo (isCurrency("10.36") ? "TRUE" : "FALSE");
echo (isCurrency(10.3) ? "TRUE" : "FALSE");
Beware of:
this function accepts also negative currency values, if you don't want to accept them remove the -?
that you find at the beginning of the preg_match
this function returns TRUE
also for values like 13.7
(only one decimal), if you want the decimal to be always two than replace {1,2}
with {2}
(but prior doing this read point 3 below)
this function returns TRUE
also for values like 0000.78
(more than one leading zero), but usually this kind of function is needed to filter value submitted by forms before inserting them into DB fields of type DECIMAL(n.2)
. When inserting in these kind of fields MySQL accepts both 0000.78
and 13.7
as valid values in query, and auto converts them into 0.78
and 13.70
Based on Tim Pietzcker great answer above, I created:
/* Sample usage:
* ============
* echo '<ol>';
* echo '<li>', (isCurrency('10.36') ? "TRUE" : "FALSE"), '</li>'; // 1 - TRUE
* echo '<li>', (isCurrency('10.3') ? "TRUE" : "FALSE"), '</li>'; // 2 - TRUE
* echo '<li>', (isCurrency('10.') ? "TRUE" : "FALSE"), '</li>'; // 3 - TRUE
* echo '<li>', (isCurrency('10') ? "TRUE" : "FALSE"), '</li>'; // 4 - TRUE
* echo '<li>', (isCurrency('1,088,888,888,888') ? "TRUE" : "FALSE"), '</li>'; // 5 - TRUE
* echo '<li>', (isCurrency('1,088888,888,888') ? "TRUE" : "FALSE"), '</li>'; // 6 - I'm not sure if this should return TRUE or FALSE. For now going with TRUE as we can still interpret the desired value regardless whether the commas are correct.
* echo '<li>', (isCurrency('$10') ? "TRUE" : "FALSE"), '</li>'; // 7 - TRUE
* echo '<li>', (isCurrency('-10') ? "TRUE" : "FALSE"), '</li>'; // 8 - TRUE
* echo '<li>', (isCurrency('$-10') ? "TRUE" : "FALSE"), '</li>'; // 9 - TRUE
* echo '<li>', (isCurrency('-$10') ? "TRUE" : "FALSE"), '</li>'; // 10 - TRUE
* echo '<li>', (isCurrency('--$10') ? "TRUE" : "FALSE"), '</li>'; // 11 - FALSE
* echo '<li>', (isCurrency('-$$10') ? "TRUE" : "FALSE"), '</li>'; // 12 - FALSE
* echo '<li>', (isCurrency('10x') ? "TRUE" : "FALSE"), '</li>'; // 13 - FALSE
* echo '<li>', (isCurrency('x10') ? "TRUE" : "FALSE"), '</li>'; // 14 - FALSE
* echo '</ol>'; */
function isCurrency (string $currencyToTest): bool
{
$regExpPattern = '/^[$-]{0,2}\d{1,3}(?:,?\d{3})*(?:\.\d{0,2})?$/';
/* $regExpPattern explanation
/^ # string must begin with
[$-](0,2) # optional $ and/or -
\d{1,3} # 1-3 digits
(?: # followed by this group...
,? # an optional comma
\d{3} # exactly three digits
)* # ...any number of times
(?: # followed by this group...
\. # a literal dot
\d{2} # exactly two digits
)? # ...zero or one times
$/ # must end with
*/
$currencyToTest = trim ($currencyToTest);
return preg_match ($regExpPattern, $currencyToTest);
}
Added:
Without Tim Pietzcker response, not sure I would have gotten this far.
You can't be sure. The user might be thinking of chickens. You need to check if the user entered a float. Have a look at preg_match: http://jp2.php.net/manual/en/function.preg-match.php
You also need to ask yourself if you want to try to work with what the user entered, or if you want to reject it and only accept an exact 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