简体   繁体   中英

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 change. 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)增加了吞吐量

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM