A summary of the two methods of JS judging whether characters are Chinese characters

Source: Internet
Author: User
Tags regular expression strlen

  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; }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.