Reputation: 705
We have a system (using ASP.NET C# 4.0) that supports Greek, Cyrillic, Chinese characters. But a third party system doesn't seem to work correctly. To avoid issues when entering data for this third party system, I want to limit the text fields to accept only English or accented characters, but return a validation error for other characters.
How can I accomplish this? It seems I can use a regex along the lines of \p{Latin}, but C# doesn't seem to support this from my experience, as I get an Unknown property 'Latin'
error.
Upvotes: 4
Views: 1986
Reputation: 44259
In .NET, the Unicode block properties need to be written with Is...
:
[\p{IsGreek}\p{IsCyrillic}...]
A pattern like this would detect all offending characters in your case. If you just want to exclude everything but Latin
, you could do something like:
[^\p{IsBasicLatin}\p{IsLatin-1Supplement}\p{IsLatinExtended-A}\p{IsLatinExtended-B}]
This covers all code points up to U+024F
.
For a list of supported block names, see MSDN.
Upvotes: 5