PHP Validating U.S. Currency – String Function

<?php
$regex = "/^\\$?(\d{1,3}(,\d{3})*|\d+)\.\d\d$/"; $values = array(
    "1,123.00",
    "1123.00"
); foreach ($values as $value) {
    if (preg_match($regex, $value)) {
        echo "'" . $value . "' is a valid number.\n";
    } else {
        echo "'" . $value . "' is NOT a valid number.\n";
    }
}
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.