Codeigniter form validation. Alpha and spaces

前端 未结 6 666
逝去的感伤
逝去的感伤 2021-01-04 21:23

When using Codeigniter form validation, does alpha allow spaces? Ex. \"Bob Smith\"

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 21:50

    I know I'm late to answer this. But for those who are still looking for an answer on how to just allow letters and white spaces, you can follow this:

    In form validation

    $this->form_validation->set_rules('fullname', 'Fullname', 'min_length[7]|trim|required|xss_clean|callback_alpha_dash_space');
    

    Then add a callback function for alpha_dash_space

    function alpha_dash_space($fullname){
        if (! preg_match('/^[a-zA-Z\s]+$/', $fullname)) {
            $this->form_validation->set_message('alpha_dash_space', 'The %s field may only contain alpha characters & White spaces');
            return FALSE;
        } else {
            return TRUE;
        }
    }
    
    • ^ and $ Tells that it is the beginning and the end of the string
    • a-z are lowercase letters, A-Z are uppercase letters
    • \s is whitespace and + means 1 or more times.

    Hope it helped!

提交回复
热议问题