Form validation pattern and utf

Notice: This thread is very old.

6 years ago

kedrigern
Member | 102
+
0
-

Hi,

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
+
0
-

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

6 years ago

MartyIX
Member | 219
+
0
-

You may encounter the following error:

http://stackoverflow.com/…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)