JAVASCRIPT: Validating an alpha input field in an internation/multilangual project

JAVASCRIPT: Validating an alpha input field in an internation/multilangual project


Validating an alpha input field in an internation/multilangual project, is not easy as it seems.

Javascript does not support unicode out of the box.
Regex like /^[A-Za-z]$/ would never work with strings like 愚蠢的人讀了 or успокойсяЯпростопошутил

At the beginning I used something like:

It works, but everybody knows how important is to keep the code highly readable and as short as possible.
Languages like PHP have p{L} as equivalent of the previous poem, what about JS?

FINAL SOLUTION
I found a beautiful lib : https://github.com/slevithan/xregexp

We can simply use it like;
let myRegexp = XRegExp(‘^\\pL+$’);

This compiles in a standard js regex, plus the lib has a lot of really useful things, check it out.

 

Leave a Reply

Your email address will not be published. Required fields are marked *