2000a Posted October 20, 2010 Share Posted October 20, 2010 Hi, How can I check if a variable only has these characters: - number (0-9) - decimal point (.) - positive (+) - negative (-) - dollar ($) I tried is_numeric, but it doesn't like negative values... I need to add the variable value to another variable. I have to accept numbers (including negative and decimals)... ex: 0.1 = TRUE -.1 = TRUE 12 = TRUE 1000.00 = TRUE 12a = FALSE thanks Link to comment https://forums.phpfreaks.com/topic/216378-check-if-variable-is-number-decimal-positivenegative/ Share on other sites More sharing options...
The Little Guy Posted October 20, 2010 Share Posted October 20, 2010 You could do it this way (I wasn't sure if spaces were allowed so I added them to the array): <?php function is_good($str){ $allowed = array('0','1','2','3','4','5','6','7','8','9', '.', '+','-', '$', ' '); $arr = str_split($str); foreach($arr as $itm){ $itm = (string)$itm; if(!in_array($itm, $allowed, true)) return false; } return true; } $str = '$12 + 12.01 -.01'; if(is_good($str)){ echo 'good'; }else{ echo 'bad'; } ?> Link to comment https://forums.phpfreaks.com/topic/216378-check-if-variable-is-number-decimal-positivenegative/#findComment-1124425 Share on other sites More sharing options...
2000a Posted October 20, 2010 Author Share Posted October 20, 2010 thanks The Little Guy. Is there a way to do it on one line? I have this working with the exception of negative+positive values (ignore the $): if(ereg("^[.0-9]+$", $myVar)) { return TRUE; } If I add "\-" it still doesn't like "-".. So if myVar="-.01", I want it to return TRUE. thanks Link to comment https://forums.phpfreaks.com/topic/216378-check-if-variable-is-number-decimal-positivenegative/#findComment-1124440 Share on other sites More sharing options...
berridgeab Posted January 21, 2011 Share Posted January 21, 2011 As this is the 3rd result in the google search results, I thought I'd post the answer I found lurking on some other website. To validate a True Decimal Number. //if the number is not a whole number then its a decimal. function validateTrueDecimal($v) { return(floor($v) != $v); } //A Small Unit Test $test = array (0.5, 0.12, 0.56, 1, 10, "Apples", -13.4, -5.12178934123487123491912471249124, 1e7, "1e7", "1.2"); foreach($test as $value) { if (validateTrueDecimal($value)==true) { echo $value . " - Number is a decimal<br>";} else {echo $value . " - <b>Number is not a decimal</b><br>";} } Output of Code - 0.5 - Number is a decimal 0.12 - Number is a decimal 0.56 - Number is a decimal 1 - Number is not a decimal 10 - Number is not a decimal Apples - Number is not a decimal -13.4 - Number is a decimal -5.1217893412349 - Number is a decimal 10000000 - Number is not a decimal 1e7 - Number is not a decimal 1.2 - Number is a decimal Link to comment https://forums.phpfreaks.com/topic/216378-check-if-variable-is-number-decimal-positivenegative/#findComment-1163117 Share on other sites More sharing options...
berridgeab Posted January 24, 2011 Share Posted January 24, 2011 Scrap the above, further testing shows it doesn't work on 10.00, 20.00, 30.00, 1.00, etc. Back to the drawing board.... Link to comment https://forums.phpfreaks.com/topic/216378-check-if-variable-is-number-decimal-positivenegative/#findComment-1164455 Share on other sites More sharing options...
litebearer Posted January 24, 2011 Share Posted January 24, 2011 The "sledge-hammer" approach... <?PHP $os = "abc $ 123.4 + - wowoow#"; function CheckThisNumber($os) { /* first remove all invalid characters */ $string = preg_replace("/[^0-9\|\.\-\+\$]/", "", $os); /* next compare original to new - if not the same then bad */ if($string != $os) { return FALSE; } /* count the occurrences of +, -, . and $ */ $c_minus = substr_count($os,"-"); $c_plus = substr_count($os,"+"); $c_dec = substr_count($os,"."); $c_dollar =substr_count($os,"$"); /* if there are both - and + then bad */ if($c_minus>0 AND $c_plus>0) { return FALSE; } /* if there are more than one occurrance of + or - or . or $ then bad */ if($c_minus>1 OR $c_plus>1 OR $c_dec>1 OR $c_dollar>1) { return FALSE; } /* there is a + or - AND it is NOT the first character then bad */ if(($c_minus==1 and $os[0] !="-") OR ($c_plus==1 and $os[0] !="+")) { return FALSE; } /* if there is a - or + AND there is a $ AND the $ is not the 2nd character then bad */ if(($c_minus==1 and $c_dollar==1 and $os[1] !="$") OR ($c_plus==1 and $c_dollar==1 and $os[1] !="$")) { return FALSE; } /* if there is NO - AND NO + BUT there is a $ AND the $ is not the 1st character then bad */ if($c_minus<1 and $c_plus<1 and $c_dollar ==1 and $os[0] !="$") { return FALSE; } return TRUE; } echo $os . "<br>"; if(!CheckThisNumber($os)) { echo "BAD"; }else{ echo "GOOD"; } echo "<br>"; $os = "-$1234.56"; echo $os . "<br>"; if(!CheckThisNumber($os)) { echo "BAD"; }else{ echo "GOOD"; } echo "<br>"; $os = "$-1234.56"; echo $os . "<br>"; if(!CheckThisNumber($os)) { echo "BAD"; }else{ echo "GOOD"; } echo "<br>"; $os = "-$$1234.56"; echo $os . "<br>"; if(!CheckThisNumber($os)) { echo "BAD"; }else{ echo "GOOD"; } ?> Link to comment https://forums.phpfreaks.com/topic/216378-check-if-variable-is-number-decimal-positivenegative/#findComment-1164494 Share on other sites More sharing options...
Vander Posted January 24, 2011 Share Posted January 24, 2011 I've just done a wee modifcation of the code berridgeab was working on, it seems to be working a bit better than his, and might be able to help you out as well. <?php $test = array (+0.5, 0.12, 0.56, 1, 10, "Apples", -13.4, -5.12178934123487123491912471249124, 1e7, "1e7","$654.43","$4,324.43", "1s7", "1.2"); foreach($test as $value): if(substr($value,0,1) == '$'): $value = substr($value,1); endif; $value = str_replace(',','',$value); $oldvalue = $value; if(!is_numeric($value)): settype($value,'float'); endif; if($value === $oldvalue): echo "{$oldvalue} - Number is a decimal<br>"; else: echo "{$oldvalue} - Number is NOT a decimal<br>"; endif; endforeach; ?> And it returns the following. 0.5 - Number is a decimal 0.12 - Number is a decimal 0.56 - Number is a decimal 1 - Number is a decimal 10 - Number is a decimal Apples - Number is NOT a decimal -13.4 - Number is a decimal -5.1217893412349 - Number is a decimal 10000000 - Number is a decimal 1e7 - Number is a decimal 654.43 - Number is a decimal 4324.43 - Number is a decimal 1s7 - Number is NOT a decimal 1.2 - Number is a decimal Edit: I refined it a little to accept "$" and commas, the only reason 1e7 seems to be accepted, is 'cause it's the same as "10,000,000". Link to comment https://forums.phpfreaks.com/topic/216378-check-if-variable-is-number-decimal-positivenegative/#findComment-1164496 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.