Changeset 534 for trunk/lib/Validator.inc.php
- Timestamp:
- Jul 27, 2015 7:56:08 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Validator.inc.php
r523 r534 120 120 * 121 121 * @param string $val The input data to validate. 122 * @param bool $negative_ok If the value can be unsigned. 122 123 * @return bool true if value is a float 123 124 */ … … 126 127 $pattern = $negative_ok ? '/^-?[[:digit:]]*(?:\.?[[:digit:]]+)$/' : '/^[[:digit:]]*(?:\.?[[:digit:]]+)$/'; 127 128 return '' == trim((string)$val) || (is_numeric($val) && preg_match($pattern, $val)); 129 } 130 131 /** 132 * Check whether input is a Decimal or Fixed type. Check values to be stored in mysql decimal, numeric, num, or fixed types. 133 * Note: some integers and floats will also pass this test. 134 * https://dev.mysql.com/doc/refman/5.5/en/fixed-point-types.html 135 * 136 * @param string $val The input data to validate. 137 * @param bool $negative_ok If the value can be unsigned. 138 * @param int $max Total max number of digits (for mysql max is 65). 139 * @param int $dec Total max number of digits after the decimal place (for mysql max is 30). 140 * @return bool true if value is a float 141 */ 142 static public function isDecimal($val, $negative_ok=false, $max=10, $dec=2) 143 { 144 if ('' == trim((string)$val)) { 145 return true; 146 } 147 if (!$negative_ok && is_numeric($val) && $val < 0) { 148 return false; 149 } 150 // Get the length of the part after any decimal point, or zero. 151 $num_parts = explode('.', $val); 152 $dec_count = sizeof($num_parts) <= 1 ? 0 : mb_strlen(end($num_parts)); 153 // Must be numeric, total digits <= $max, dec digits <= $dec. 154 return is_numeric($val) && mb_strlen(str_replace(['-', '.'], '', $val)) <= $max && $dec_count <= $dec; 128 155 } 129 156
Note: See TracChangeset
for help on using the changeset viewer.