I agree with Billiam that you should extend the Form_validation class
I believe it is more likely that you will want to check the whitelist of possible string values, rather than the blacklist. For example, you know that the field 'shirt_size' should only return string values: 'xl', 'l', 'm', 's'. My solution is to handle both cases.
I use these methods in MY_From_validation:
/** * ENUM * The submitted string must match one of the values given * * usage: * enum[value_1, value_2, value_n] * * example (any value beside exactly 'ASC' or 'DESC' are invalid): * $rule['order_by'] = "required|enum[ASC,DESC]"; * * example of case-insenstive enum using strtolower as validation rule * $rule['favorite_corey'] = "required|strtolower|enum[feldman]"; * * @access public * @param string $str the input to validate * @param string $val a comma separated lists of values * @return bool */ function enum($str, $val='') { if (empty($val)) { return FALSE; } $arr = explode(',', $val); $array = array(); foreach($arr as $value) { $array[] = trim($value); } return (in_array(trim($str), $array)) ? TRUE : FALSE; } // -------------------------------------------------------------------- /** * NOT ENUM * The submitted string must NOT match one of the values given * * usage: * enum[value_1, value_2, value_n] * * example (any input beside exactly 'feldman' or 'haim' are valid): * $rule['favorite_corey'] = "required|not_enum['feldman','haim']"; * * @access public * @param string $str the input to validate * @param string $val a comma separated lists of values * @return bool */ function not_enum($str, $val='') { return ($this->enum($str,$val) === TRUE)? FALSE : TRUE; }
Using the Billiam example, the validation rule does not allow the string "invalid value":
trim|alpha_numeric|not_enum[invalid value]
source share