dadamssg87 Posted May 5, 2011 Share Posted May 5, 2011 I'm trying to develop a function to check to see if the value inputted was in a currency format. I'm checking for: if the value is numeric if the value is an integer if not integer check the number of digits to the right of the decimal point I can't tell how the php function is_int() works though. I want to accept integers and also numbers with two decimal places. So accepted values will be "123" and also "123.99". The script tells me the value is not an integer no matter what. <?php $num = $_POST['number']; $positionOfDecimalPoint = strpos($num, '.'); $numbersToLeft = substr($num,0,$positionOfDecimalPoint); $numbersToRight = substr($num,$positionOfDecimalPoint + 1);//Add one so decimal point isn't included in output. echo "You submitted ".$num."<br><br>"; if(!is_numeric($num)) { echo "Not Numeric"; } else { if(!is_int($num)) { $adecimal = strlen($numbersToRight); if($adecimal !== 2) { echo "There needs to be exactly two numbers after the decimal."; } echo "<br><br>The number is not an integer."; }else { echo "The integer is ".$num; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235650-check-for-currency/ Share on other sites More sharing options...
dadamssg87 Posted May 6, 2011 Author Share Posted May 6, 2011 got it with this...accepts integers and only numbers with two decimal points. <?php function check_currency($num) { if(!is_numeric($num)) { echo "Not Numeric"; } else { $decimal_exists = strpos($num, '.'); if($decimal_exists == TRUE) { $after_decimal = strlen(substr($num, $decimal_exists + 1)); if($after_decimal !== 2) { echo "There needs to be exactly two digits after the decimal."; } else { echo "You entered the correct format for a decimal."; } } else { echo "You entered an acceptable integer."; } } } Quote Link to comment https://forums.phpfreaks.com/topic/235650-check-for-currency/#findComment-1211206 Share on other sites More sharing options...
Pikachu2000 Posted May 6, 2011 Share Posted May 6, 2011 is_numeric() returns TRUE for 12E467 and 0xAF5579CDB3 also. This validation may be better handled with a regex pattern. Quote Link to comment https://forums.phpfreaks.com/topic/235650-check-for-currency/#findComment-1211306 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.