如何验证中文(unicode)和英文名称? [英] How to validate both Chinese (unicode) and English name?

查看:131
本文介绍了如何验证中文(unicode)和英文名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多语言网站(中文和英文)。

I have a multilingual website (Chinese and English).

我想在javascript中验证文本字段(名称字段)。到目前为止,我有以下代码。

I like to validate a text field (name field) in javascript. I have the following code so far.

var chkName = /^[characters]{1,20}$/;

if( chkName.test("[name value goes here]") ){
  alert("validated");
}

问题是,/ ^ [字符] {1,20} $ /只匹配英文字符。是否可以匹配任何(包括unicode)字符?我以前使用以下正则表达式,但我不想在每个characeters之间留出空格。

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.

/^(.+){1,20}$/


推荐答案

你可能会检查 Javascript + Unicode正则表达式并进行一些研究以准确找到您想要允许的字符范围:

You might check out Javascript + Unicode regexes and do some research to find exactly which ranges of characters you want to allow:

请参阅什么是Unicode中的汉字的完整范围?

在阅读了这两个以及一些额外的研究后,您应该能够找到合适的值来完成以下内容: / ^ [ - 'az \ u4e00-\ u9eff] {1,20} $ / i

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

这篇关于如何验证中文(unicode)和英文名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆