Recently, I came across an interesting validation issue while working with a web form.
The application used the following regular expression:
At first glance, this looks like a simple way to allow letters, numbers, spaces, and a few special characters.
But there is an important limitation: it only allows the English alphabet.
What happens with German characters?
Consider the following value:
The character ü is not included in:
As a result, the regular expression rejects the value.
The same problem can occur with other characters:
These characters are perfectly valid in many languages, but they are not part of the basic ASCII alphabet.
ASCII vs. Unicode
ASCII is a limited character set that includes the English alphabet, numbers, and common symbols.
Unicode is much broader and supports characters from languages around the world.
For example:
ASCII:
Baden-Wuerttemberg
Unicode:
Baden-Württemberg
Both represent the same place, but a strict ASCII-based regex treats them differently.
A More Flexible Approach
Modern JavaScript supports Unicode property escapes.
For example:
/^[\p{L}\p{N} &.()-]+$/u
Here:
-
\p{L} represents Unicode letters
-
\p{N} represents Unicode numbers
-
u enables Unicode-aware matching
This makes the expression much more suitable for international text.
Of course, the right validation rule depends on the purpose of the field. A username, an AWS resource identifier, and a person's location should not necessarily use the same validation rules.
The Main Lesson
This experience reminded me that validation rules should match the type of data they are validating.
If an application supports international users, restricting text to A-Z and a-z can create unexpected problems.
A dropdown may provide a perfectly valid value such as Baden-Württemberg, but a backend validation rule can still reject it if Unicode characters are not supported.
For me, this was a small but useful lesson about the importance of understanding regex, ASCII, and Unicode when working with web applications.
Have you encountered similar Unicode or regex validation issues?
Loading article