0 Replies - 1087 Views - Last Post: 09 September 2011 - 02:03 PM

#1 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 236
  • Joined: 20-February 11

validateFloatPlaces

Posted 09 September 2011 - 02:03 PM

Description: Plug and play.Validates a floating point number to $x digits before the decimal place, and $y digits after.
    
/**
 *validates a floating point number to $before digits before the decimal
 *place, and $after digits after the decimal place. 
 *
 * @param $float float number to be validated
 * @param $before int number of digits allowed before decimal place
 * @param $after int number of digits allowed after decimal place
 * @return bool true if valid, false otherwie
 */
function validateFloatPlaces($float, $before = 2, $after = 3)
    {
        $regex = '/^';

        for($i=0; $i < $before; $i++) {
            $regex .= 'd';
        }

        $regex .= '.';

        for($i = 0; $i < $after; $i++) {
			if($i == ($after - 1)) {
				$regex .= 'd$';
			} else {
				$regex .= 'd';
			}
        }

        $regex .= '/';
		
        if(preg_match($regex, $float) == 0) {
            return false;
        }
        return true;
    }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1