IvanK
IvanK

Reputation: 65

CodeIgniter Form Validation for Chinese Words

I am using library of Form Validation in CodeIgniter. Below config try to include all Number, English words, Chinese words and space. But it's not work.

$config = array(
                array(
                       'field' => 'keywords',
                       'label' => 'keywords',
                       'rules' => 'regex_match[/[a-zA-Z0-9 \u4e00-\u9fa5]+$/]'
                    )
                );

However, if I deduce '\u4e00-\u9fa5', it's work.

$config = array(
                    array(
                           'field' => 'keywords',
                           'label' => 'keywords',
                           'rules' => 'regex_match[/[a-zA-Z0-9 ]+$/]'
                        )
                    );

Upvotes: 2

Views: 529

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626806

There are three issues in the regex you have:

  • The validation regex should start matching at the start of the string, thus, you need the start of string anchor ^ or \A. Also, it is advisable to replace $ with the very end of the string anchor \z (as $ also matches before the final newline symbol in a string).
  • Revo is right, \uXXXX notation is not supported by PHP regex engine. However, you do not have to specify the range of Unicode code points here. Chinese characters in PHP PCRE regex can be defined with a Unicode property \p{Han}.
  • For a PCRE regex to become Unicode aware, you need to use the /u modifier.

So, use

/\A[a-zA-Z0-9\s\p{Han}]+\z/u

Or (a tiny bit less secure),

/^[a-zA-Z0-9\s\p{Han}]+$/u

Upvotes: 1

revo
revo

Reputation: 48711

PCRE does not support the \uFFFF syntax. Use \x{FFFF} instead.

/[a-zA-Z0-9 \x{4e00}-\x{9fa5}]+$/

Upvotes: 0

Related Questions