1

I have an online leaderboard where players come from all over the world to enter their score. In my app, I want to restrict the player's name to English alphanumeric and foreign language characters but no special symbol allowed.

This data is saved into an online mysql database.

I know I can check for English alphanumeric using the following

  string correctedName = Regex.Replace(settings.PlayerNameSetting, @"[^A-Za-z0-9 ]+", "");

but how about allowing to accept Japanese, Chinese, Spanish or other languages characters too?

PutraKg
  • 2,226
  • 3
  • 31
  • 60

1 Answers1

0

In .Net,

^[\p{L}\d_][\p{L}\d_.\s-]*$

is equivalent to your regex, additionally allowing other Unicode letters.

Explanation:

\p{L} is a shorthand for the Unicode property "Letter".

Caveat: I think you wanted to not allow the underscore as initial character (evidenced by its presence only in the second character class). Since \w includes the underscore, your regex did allow it, though. You might want to remove it from the first character class in my solution (it's not included in \p{L}, of course).

In ECMAScript, things are not so easy. You would have to define your own Unicode character ranges. Fortunately, a fellow StackOverflow user has already risen to the occasion and designed a JavaScript regex converter:

https://stackoverflow.com/a/8933546/20670

Community
  • 1
  • 1