Às vezes, é necessário determinar se um caractere é um caractere chinês. Geralmente, existem dois métodos para julgar com JavaScript.
1. Use uma expressão regular para julgar
Copie o código do código da seguinte forma:
<!
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "content-type" content = "text /html; charset = gb2312" /> />
<Title> JS determina se os personagens são caracteres chineses </title>
<Style type = "text/css">
.contente {{
Largura: 350px;
estouro: oculto;
Fronteira: 1px sólido #DDD;
}
</style>
<Script Language = "Javascript" type = "text/javascript">
Função de verificação (obj, valor) {
var reg = novo regexp ("[// u4e00- // u9fff]+", "g");
if (reg.test (val)) {
Alerta ("Não é possível entrar em caracteres chineses!");
var strobj = document.getElementById (OBJ);
strobj.value = "";
Strobj.focus ();
}
}
</script>
</head>
<Body>
<div>
<div> caracteres de teste: <input id = "test" type = "text" onblur = "checkchinese ('teste', this.value)" /> </div>
</div>
</body>
</html>
2. Julgando o alcance do caráter unicode
O método a seguir é usado para estatísticas o comprimento da sequência de entrada.
Copie o código do código da seguinte forma:
Função chktrlen (str)
{{
var strlen = 0;
for (var i = 0; i <str.Length; i ++)
{{
if (str.charcodeat (i)> 255 // Se for um caractere chinês, o comprimento da corda é mais 2
stren += 2;
outro
Strlen ++;
}
Retornar Strlen;
}