To check a user input english name is valid, I would usually match the input against regular expression such as [A-Za-z]. But how can I do this if multi-language(like Chinese, Japanese etc.) support is required with utf8 encoding?
相关问题
- Date with SimpleDateFormat in Java
- UrlEncodeUnicode and browser navigation errors
- Improve converting string to readable urls
- Regex to match charset
- Regex subsequence matching
相关文章
- Angular Material Stepper causes mat-formfield to v
- Optimization techniques for backtracking regex imp
- Regex to check for new line
- Why doesn't Django enforce my unique_together
- Allow only 2 decimal points entry to a textbox usi
- Why is `'↊'.isnumeric()` false?
- How to display unicode in SVG?
- Comparing speed of non-matching regexp
In PHP I use this nasty hack:
That includes "Umlauts" (i.e. 'ä','ö' and the like) plus accented vowels (è,í,etc.). But it falls short to validate for Cyrillic (Russia, Bulgaria, ...) or Chinese characters...
You can approximate the Unicode derived property
\p{Alphabetic}
pretty succintly with[\pL\pM\p{Nl}]
if your language doensn’t support a proper Alphabetic property directly.Don’t use Java’s
\p{Alpha}
, because that’s ASCII-only.But then you’ll notice that you’ve failed to account for dashes (
\p{Pd}
or DashPunctuation works, but that does not include most of the hyphens!), apostrophes (usually but not always one of U+27, U+2BC, U+2019, or U+FF07), comma, or full stop/period.You probably had better include
\p{Pc}
ConnectorPunctuation, just in case.If you have the Unicode derived property
\p{Diacritic}
, you should use that, too, because it includes things like the mid-dot needed for geminated L’s in Catalan and the non-combining forms of diacritic marks which people sometimes use.But then you’ll find people who use ordinal numbers in their names in ways that
\p{Nl}
(LetterNumber) doesn’t accomodate, so you throw\p{Nd}
(DecimalNumber) or even all of\pN
(Number) into the mix.Then you realize that Asian names often require the use of ZWJ or ZWNJ to be written correctly in their scripts, so then you have to add U+200D and U+200C to the mix, which are both
\p{Cf}
(Format) characters and indeed also JoinControl ones.By the time you’re done looking up the various Unicode properties for the various and many exotic characters that keep cropping up — or when you think you’re done, rather — you’re almost certain to conclude that you would do a much better job at this if you simply allowed them to use whatever Unicode characters for their name that they wish, as the link Tim cites advises. Yes, you’ll get a few jokers putting in things like “əɯɐuʇƨɐ⅂ əɯɐuʇƨɹᴉℲ”, but that just goes with the territory, and you can’t preclude silly names in any reasonable way.
Think about whether you really need to validate the user's name. Maybe you should let users call themselves whatever they want.
You certainly should never use
[A-Za-z]
, because some people have names with apostrophes or hyphens. It can be quite insulting to prevent someone from using their real name just because it doesn't follow your arbitrary rules for what a name should look like.