Yahya KACEM
Yahya KACEM

Reputation: 2481

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.

Upvotes: 0

Views: 1382

Answers (2)

Alfonso Rubalcava
Alfonso Rubalcava

Reputation: 2247

This is an old question, but the accepted answer is incomplete. The search pattern \pL is affected by the setLocale configuration. For it to work everywhere, you have to use unicode. Something like that:

public function alpha_dash($str){
    return (bool) preg_match('/^[0-9\pL _-]+$/u',$str);
}

Upvotes: 0

Toto
Toto

Reputation: 91415

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

Upvotes: 3

Related Questions