Home > Web Front-end > JS Tutorial > How to restrict users to input Chinese characters using javascript_javascript skills

How to restrict users to input Chinese characters using javascript_javascript skills

WBOY
Release: 2016-05-16 16:30:52
Original
1681 people have browsed it

The example in this article describes the method of javascript restricting users to only input Chinese characters. Share it with everyone for your reference. The specific implementation method is as follows:

When we want to verify the function, we must understand that if it is a Chinese character, the string length is increased by 2. If we use regular expressions, we can directly use \u4E00-\u9FA5 to solve the problem.

1. Unicode check Chinese characters

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, add 2 to the string length
    strlen = 2;
   else
    strlen ;
  }
return strlen;
}

2. Only Chinese characters can be input using regular expressions

Copy code The code is as follows:

I hope this article will be helpful to everyone's JavaScript programming design.
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