codeigniter alpha_numeric form validation not accepting space

大城市里の小女人 提交于 2019-12-13 04:37:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!