15

I need to validate a text input so a user can insert characters/text that may include German umlauts, French accents and any other valid European characters, for example the minuscule ø.

I am using AngularJS so I am applying my validation rule to the ng-pattern attribute like so:

ng-pattern="/^[A-Za-z0-9 \-_.]*$/"

I was hoping this would cover characters like äöüß but when testing it doesn't. Sorry to ask such a lame question but I am really bad at RegEx! There must be a better way than manually listing the letters like so ng-pattern="/^[A-Za-z0-9äöüÄÖÜ \-_.]*$/"

Mike Sav
  • 14,805
  • 31
  • 98
  • 143
  • Maybe you can use the external lib [XRegExp](http://xregexp.com/) with the [Unicode plugin](http://xregexp.com/plugins/), for more details, see my answers [here](http://stackoverflow.com/a/17358230/626273), [here](http://stackoverflow.com/a/7752816/626273) and [here](http://stackoverflow.com/a/7578937/626273). – stema Sep 25 '13 at 10:08
  • Why? Are you going to know all scripts used before? What about european languages typed in non-latin scripts? – sapht Nov 12 '17 at 12:03

4 Answers4

22

Javascript regexes don't support unicode properties, the only way to include non-latin letters is to list them explicitly in your expression:

 [A-Za-z0-9 \-_.äöüß etc]

or use unicode ranges, e.g

 [A-Za-z0-9 \-_.\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF]

(see http://www.utf8-chartable.de/ for reference).

georg
  • 211,518
  • 52
  • 313
  • 390
22

[a-zA-Z\x7f-\xff] contains a lot of special characters such as ä ö ü é ß î...

Tobias Mühl
  • 1,788
  • 1
  • 18
  • 30
0

I faced an issue with finding a Markdown Pattern within a string but it crashed if the text contained an umlaut or ß .

Encoding the text actually helped thx to this extension

No more crashes and works great.

Rikco
  • 376
  • 5
  • 9
-3

The following worked for me for spaces, and special characters, without numbers ^[a-zA-ZÀ-ž ]*$

Floern
  • 33,559
  • 24
  • 104
  • 119