Changeset 2592

Show
Ignore:
Timestamp:
04/28/2008 03:01:02 PM (5 months ago)
Author:
Shadowhand
Message:

Added valid::decimal, updated comments.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/valid.php

    r2519 r2592  
    241241 
    242242        /** 
    243          * Checks whether a string is a valid text. 
     243         * Checks whether a string is a valid text. Letters, numbers, whitespace, 
     244         * dashes, periods, and underscores are allowed. 
    244245         * 
    245246         * @param   string   $str 
     
    251252        } 
    252253 
     254        /** 
     255         * Checks if a string is a proper decimal format. The format array can be 
     256         * used to specify a decimal length, or a number and decimal length, eg: 
     257         * array(2) would force the number to have 2 decimal places, array(4,2) 
     258         * would force the number to have 4 digits and 2 decimal places. 
     259         * 
     260         * @param   string   input string 
     261         * @param   array    decimal format: y or x,y 
     262         * @return  boolean 
     263         */ 
     264        public function decimal($str, $format = NULL) 
     265        { 
     266                // Create the pattern 
     267                $pattern = '/^[0-9]%s\.[0-9]%s$/'; 
     268 
     269                if ( ! empty($format)) 
     270                { 
     271                        if (count($format) > 1) 
     272                        { 
     273                                // Use the format for number and decimal length 
     274                                $pattern = sprintf($pattern, '{'.$format[0].'}', '{'.$format[1].'}'); 
     275                        } 
     276                        elseif (count($format) > 0) 
     277                        { 
     278                                // Use the format as decimal length 
     279                                $pattern = sprintf($pattern, '+', '{'.$format[0].'}'); 
     280                        } 
     281                } 
     282                else 
     283                { 
     284                        // No format 
     285                        $pattern = sprintf($pattern, '+', '+'); 
     286                } 
     287 
     288                return (bool) preg_match($pattern, (string) $str); 
     289        } 
     290 
    253291} // End valid