Codeigniter validation form does not allow accents and other "br" characters

Asked

Viewed 477 times

2

I’m using the rule "alpha_numeric_spaces" to validate letters and numbers with space, but when I use some special character or letters like "ç", the validation does not pass, there is some way around this or just creating a special rule?

Ex:

 $validator->set_rules('nome', 'Nome', 'min_length[3]|max_length[60]|alpha_numeric_spaces');

1 answer

2


You need to create your own rule. Just look at the implementation alpha_numeric_spaces codeigniter:

public function alpha_numeric_spaces($str) {
    return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
}

That is to say, insensitively (/i) will validate strings containing only 1 or more (+$) letters from A to Z, digits from 0 to 9 or spaces. And you will need to \p{L} in the validation regex of its input, to pass any accented character of the Latin alphabet, including c:

$validator->set_rules('nome', 'Nome', array('trim', 'regex_match[/[\p{L}0-9 ]+$/i]'));

Otherwise you can just add the c within the rule: [A-Z0-9Ç ].

  • Thanks, but where do I put this function? I have to create one class and extend another and redeclarate the function?

  • 1

    Hello! The function that has been demonstrated does not need to put anywhere, it is already in the sources of Codeigniter, it is only for appreciation. What is needed is to rewrite the validation line of your input, removing your alpha_numeric_spaces and inserting regex_match[/[\p{L}0-9 ]+$/i].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.