Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate UTF-8 names with angularjs and ngPattern

Is there a way to use ng-pattern in form validation to validate any letter from any alphabet (latin, chinese, korean, russian, ...).

I found a solution with the XRegExp library, but will not work with ng-pattern since it is expecting a string regex.

XRegExp("^\\p{L}[\\p{L} ']*$")

One solution is too build my own validator directive, but I would prefer to use ng-pattern if possible.

like image 257
Laurent Avatar asked Aug 16 '26 01:08

Laurent


1 Answers

You need to add the u flag for Unicode support. That way you can specify patterns that include character categories, such as \p{L} for a letter in any language, \p{M} for an accent mark in any language.

Example:

ng-pattern="/^[\\p{L}\\p{M}][\\p{L}\\p{M}\\p{Zs}'\\-]*$/u"

Explanation:

  • [\p{L}\p{M}] -- any letter in any alphabet, including accent mark
  • \p{Zs} -- space separator in any language
  • above pattern allows space, ', and - at the end, you can tweak the pattern if you want to avoid that

See details on Unicode and character categories at https://javascript.info/regexp-unicode

like image 75
Peter Thoeny Avatar answered Aug 18 '26 17:08

Peter Thoeny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!