Home > Web Front-end > JS Tutorial > Summary of two ways to determine whether a character is a Chinese character in js_javascript skills

Summary of two ways to determine whether a character is a Chinese character in js_javascript skills

WBOY
Release: 2016-05-16 17:05:39
Original
1587 people have browsed it

Sometimes it is necessary to determine whether a character is a Chinese character. For example, when the user inputs content containing Chinese and English, it needs to be used to determine whether it exceeds the specified length. There are usually two ways to judge using Javascript.

1. Use regular expressions to determine

Copy the code The code is as follows:




 
 js determines whether the character is a Chinese character
 




Test character:

 

< ;/body>



2. Use Unicode character range to determine The following method is used to count the length of the input string. If it is a Chinese character, the string length is increased by 2; otherwise, the string length is increased by 1.


Copy code The code is as follows:
function chkstrlen(str)
 {
var strlen = 0;
 for(var i = 0;i < str.length; i )
  {
  if(str.charCodeAt(i) > 255) //If it is a Chinese character, then String length plus 2
 strlen = 2;
else
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template