diff options
author | Lance Vincent <lancevincent@gmail.com> | 2015-01-26 05:20:48 +0100 |
---|---|---|
committer | Lance Vincent <lancevincent@gmail.com> | 2015-01-26 05:20:48 +0100 |
commit | e13fea5f71c5a8ae4ace1c52483fb61b9f1b87c5 (patch) | |
tree | fe2ac763cb15f2b0ab555493085c8c7558eec3b6 | |
parent | a135a18fe99ccf4f27dabc6c4a045e42cd239cea (diff) |
Form Validation - Between method
Value must be within a given numeric range
-rw-r--r-- | system/language/english/form_validation_lang.php | 1 | ||||
-rw-r--r-- | system/libraries/Form_validation.php | 21 | ||||
-rw-r--r-- | user_guide_src/source/libraries/form_validation.rst | 1 |
3 files changed, 23 insertions, 0 deletions
diff --git a/system/language/english/form_validation_lang.php b/system/language/english/form_validation_lang.php index 9156ebef0..b7ee25a38 100644 --- a/system/language/english/form_validation_lang.php +++ b/system/language/english/form_validation_lang.php @@ -53,6 +53,7 @@ $lang['form_validation_less_than'] = 'The {field} field must contain a number l $lang['form_validation_less_than_equal_to'] = 'The {field} field must contain a number less than or equal to {param}.'; $lang['form_validation_greater_than'] = 'The {field} field must contain a number greater than {param}.'; $lang['form_validation_greater_than_equal_to'] = 'The {field} field must contain a number greater than or equal to {param}.'; +$lang['form_validation_between'] = 'The {field} field must contain a number between {param}.'; /* End of file form_validation_lang.php */ /* Location: ./system/language/english/form_validation_lang.php */
\ No newline at end of file diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index b640f1ec1..16766ca66 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1438,6 +1438,27 @@ class CI_Form_validation { // -------------------------------------------------------------------- /** + * Between two numeric values + * + * @param int + * @param string + * @return bool + */ + public function between($str, $range) + { + if ( ! preg_match('/^\s?(\d+)\s?,\s?(\d+)\s?$/', $range)) + { + return FALSE; + } + + list($range_start, $range_end) = array_map('intval', explode(',', $range)); + + return is_numeric($str) ? ($str >= $range_start && $str <= $range_end) : FALSE; + } + + // -------------------------------------------------------------------- + + /** * Is a Natural number (0,1,2,3, etc.) * * @param string diff --git a/user_guide_src/source/libraries/form_validation.rst b/user_guide_src/source/libraries/form_validation.rst index 2b7780ff2..9776d7318 100644 --- a/user_guide_src/source/libraries/form_validation.rst +++ b/user_guide_src/source/libraries/form_validation.rst @@ -962,6 +962,7 @@ Rule Parameter Description not numeric. **less_than_equal_to** Yes Returns FALSE if the form element is greater than the parameter value, less_than_equal_to[8] or not numeric. +**between** Yes Returns FALSE if the form element is not within the given range. between[10,20] **alpha** No Returns FALSE if the form element contains anything other than alphabetical characters. **alpha_numeric** No Returns FALSE if the form element contains anything other than alpha-numeric characters. **alpha_numeric_spaces** No Returns FALSE if the form element contains anything other than alpha-numeric characters |