问题
i want to set form validation so that user cannot add special characters like $, @, &, etc... only alpha numeric characters are allow
$this->form_validation->set_rules('cat_name', 'Category Name', 'trim|xss_clean|required|is_unique[cht_category.cat_name]|min_length[5]|max_length[75]|alpha_numeric');
when i add category name with space then i get following error "The Category Name field may only contain alpha-numeric characters." when i add category name without space or single word then its working fine
回答1:
Use:
alpha_numeric_spaces
Ref: https://www.codeigniter.com/userguide3/libraries/form_validation.html
回答2:
Well, alpha_numeric
won't allow spaces to pass. Instead of using alpha_numeric
, you can write your own function and pass it there (Codeigniter form validation. Alpha and spaces):
function alpha_dash_space($str)
{
return ( ! preg_match("/^([-a-z_ ])+$/i", $str)) ? FALSE : TRUE;
}
and then
$this->form_validation->set_rules('cat_name', 'Category Name', 'trim|xss_clean|required|is_unique[cht_category.cat_name]|min_length[5]|max_length[75]|callback__alpha_dash_space');
来源:https://stackoverflow.com/questions/18561943/codeigniter-alpha-numeric-form-validation-not-accepting-space