Sunil
Sunil

Reputation: 21406

Regular Expression validation not working for Chinese culture in ASP.Net

I have a regular expression validator with client-side validation disabled in an ASP.Net page. The regular expression being used for this validator is as below and it is validating input into a Product Description multi-line text box.

 Expression="^[\\p .,;'\-(0-9)\(\)\[\]]+$"

The culture for this ASP.Net app is Chinese as specified in web config.

<globalization uiCulture="zh" culture="zh-CHT" />

The following input into Product Description text box in same ASP.Net page is always failing. I am trying to match any one of these: chinese langauge character or period or comma or semi-colon or single quote or digits or round/square brackets.

Question: What is in the regular expression that is causing this input text to fail and how can I change it to satisfy the matching requirements?

(1)降低庫存過程 (2)增加了吞吐量(1)降低庫存過程 (2)增加了吞吐量(1)降低庫存過程 (2)增加了吞吐量(1)降低庫存過程 (2)增加了吞吐量

Upvotes: 2

Views: 1483

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626816

In .NET regex, the one that works on server side, you can make use of Unicode categories.

^[\p{L}\p{M}\p{N}\s\p{P}]+$

See demo

So, the character class matches:

  • \p{L} - Unicode letters
  • \p{M} - diacritic marks
  • \p{N} - numbers
  • \s - whitespace
  • \p{P} - punctuation.

Note these Unicode categories won't work on client-side where your Englsh UI culture validation takes place. You can use your fixed expression there:

^[a-zA-Z .,;'\-0-9()\[\]]+$

See demo

Upvotes: 3

Related Questions