This article is mainly on the JS to determine whether the characters are Chinese characters are the two methods of a detailed summary of the introduction, the need for friends can come to the reference, I hope to help you all
Sometimes it is necessary to judge whether a character is Chinese or not, for example, when a user enters a content that contains both Chinese and English, it needs to be judged if it exceeds the specified length. There are usually two ways of using Javascript to judge. 1, the regular expression of the judgment code is as follows: <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>js Determines whether characters are Chinese characters </title> <style type=" Text/css "> . Conte nt{ width:350px; Overflow:hidden &NBS P BORDER:1PX solid #ddd; } </style> <script language= "javascript" type= "Text/javascript" > function Checkchinese (obj,val) { var reg = new RegExp ("[u4e00-u9fff]+", "G"); if (Reg.test (val)) { alert ("Cannot enter Chinese characters!") "); var strobj = DocumeNt.getelementbyid (obj); strobj.value = ""; strobj.focus (); { } </script> </head> <body> &L T;div class= "Content" > <div> test characters: <input id= "Test" type= "text" onblur= "Checkchinese (' Test ', this.value) '/> </div> </div> </body> </html> 2, using Unicode character range to determine The following method is used to count the length of the input string and, if it is Chinese, the string length plus 2, otherwise the string length plus 1. Code as follows: function Chkstrlen (str) {var strlen = 0; for (var i = 0;i < Str.length i++) {if (Str.charcodeat (i) > 255)//If it is kanji, the string length plus 2 strlen + = 2; else strlen++; return strlen; }