4

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.

4444
  • 3,541
  • 10
  • 32
  • 43
Andrew Johns
  • 705
  • 1
  • 6
  • 26

1 Answers1

5

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.

Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • Ah yes, I was looking for a list earlier but couldn't find it. I had tried IsLatin, but I see that isn't on the list, you need to be more specific. Perfect. – Andrew Johns Aug 15 '13 at 13:35