Search code examples
phpregexvalidationcodeigniter-2

accented chars form_validation codeigniter


how can i extend my existing form_validaion class to make it accept accented characters i'm using codeigniter and this is MY_Form_validation so far:

class MY_Form_validation extends CI_Form_validation{
    public function __construct(){
        parent::__construct();
    }
    public function alpha_dash($str){
        return (!preg_match("/^([-a-z0-9 _-])+$/i", $str)) ? FALSE : TRUE;
    }
}

by accented chars I mean this:

"é à è ç ê î â ô ï ö ë ä ù ..."

Thanks, in advance.


Solution

  • Just add the wanted characters in the class:

    [a-z0-9 _àèéù-]
    

    or use unicode properties:

    [\pL\pN_ -]
    

    \pL stands for any letter \pN for any digit

    Exemple:

    $str = 'abcèéù';
    echo preg_match('/^[\pL\pN_ -]+$/', $str) ? 'TRUE' : 'FALSE';
    

    output:

    TRUE