Javascript 如何同时验证中文(unicode)和英文名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6377407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to validate both Chinese (unicode) and English name?
提问by Moon
I have a multilingual website (Chinese and English).
我有一个多语言网站(中文和英文)。
I like to validate a text field (name field) in javascript. I have the following code so far.
我喜欢在 javascript 中验证文本字段(名称字段)。到目前为止,我有以下代码。
var chkName = /^[characters]{1,20}$/;
if( chkName.test("[name value goes here]") ){
alert("validated");
}
the problem is, /^[characters]{1,20}$/ only matches English characters. Is it possible to match ANY (including unicode) characters? I used to use the following regex, but I don't want to allow spaces between each characeters.
问题是, /^[characters]{1,20}$/ 只匹配英文字符。是否可以匹配任何(包括 unicode)字符?我曾经使用以下正则表达式,但我不想在每个字符之间留有空格。
/^(.+){1,20}$/
回答by searlea
You might check out Javascript + Unicode regexesand do some research to find exactly which ranges of characters you want to allow:
您可以查看Javascript + Unicode 正则表达式并进行一些研究以准确找到您想要允许的字符范围:
See What's the complete range for Chinese characters in Unicode?
After reading those two and a little extra research you should be able to find appropriate values to complete something like: /^[-'a-z\u4e00-\u9eff]{1,20}$/i
在阅读了这两个和一些额外的研究之后,您应该能够找到合适的值来完成类似的事情: /^[-'a-z\u4e00-\u9eff]{1,20}$/i
回答by yarian
回答by yarian
As of 2018, there is new syntax in JavaScript to match Chinese or any other non-ASCII scripts:
截至 2018 年,JavaScript 中有新的语法来匹配中文或任何其他非 ASCII 脚本:
const REGEX = /(\p{Script=Hani})+/gu; // note the 'u'
'你好'.match(REGEX);
// ["你好"]
The trick is to use \p
and use the right script name, Hani
stands for Han script (Chinese). The full list of scripts is here: http://unicode.org/Public/UNIDATA/PropertyValueAliases.txt
诀窍是使用\p
和使用正确的脚本名称,Hani
代表汉字(中文)。脚本的完整列表在这里:http: //unicode.org/Public/UNIDATA/PropertyValueAliases.txt
To match both Chinese and English you just expand it a bit, for example:
要匹配中文和英文,您只需将其扩展一点,例如:
const REGEX = /([A-Za-z]|\p{Script=Hani})+/gu;
// does not match accented letters though
回答by André Schappo
I have done some work on validating Chinese names using XRegExp. The core code is XRegExp("^((?![\\p{InKangxi_Radicals}\\p{InCJK_Radicals_Supplement}\\p{InCJK_Symbols_and_Punctuation}])\\p{Han}){2,4}$","u")
我已经做了一些使用 XRegExp 验证中文名称的工作。核心代码是XRegExp("^((?![\\p{InKangxi_Radicals}\\p{InCJK_Radicals_Supplement}\\p{InCJK_Symbols_and_Punctuation}])\\p{Han}){2,4}$","u")
回答by roberkules
var chkName = /\s/;
function check(name) {
document.write("<br />" + name + " is ");
if (!chkName.test(name)) {
document.write("okay");
} else {
document.write("invalid");
}
}
check("namevaluegoeshere");
check("name value goes here");
This way you just check if there's any white space in the name.
这样您只需检查名称中是否有空格。