Form validation pattern and utf

Notice: This thread is very old.

6 years ago

Member | 102


I am using regexp validation in Nette forms and I have problem with utf.

This is solution for english:

$this->addText("name", "Label:")
 ->addRule(Form::PATTERN, 'Message', '\w*');

But I need general solution (string can contains any utf letter like ěščřžýáíé).

What is the proper way?

6 years ago

David Grudl
Nette Core | 7148

Regexp in Javascript has surprisingly weak unicode support, so you must use something like [a-zA-Z0-9_\\u00A1-\\uFFFF]

6 years ago

Member | 219

You may encounter the following error:…port-l-l-n-p

when using \uhhhh in PHP because PCRE is compiled by default without Unicode support. I used \x{hhhh} and added a line to netteForms.js:

pattern: function(elem, arg, val) {
        // replace \x{1234} -> \u1234
        arg = arg.replace(/\\x\{([a-fA-F0-9]{4})\}/gi, '\\u$1'); // NEW LINE
        try {
                return typeof arg === 'string' ? (new RegExp('^(' + arg + ')$')).test(val) : null;
        } catch (e) {

and everything works all right.

Last edited by MartyIX (2015-02-25 19:36)