This article mainly introduces how to restrict users from entering Chinese characters only in javascript. The example shows how to use Unicode and regular expressions to determine whether a user can input Chinese characters, for more information about how to restrict users to Chinese characters, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:
To verify the function, we must understand that if it is a Chinese character, the string length is increased by 2. If it is a regular expression, we can use \ u4E00-\ u9FA5 directly.
I. Unicode Chinese Characters
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, the length of the string is increased by 2
Strlen + = 2;
Else
Strlen ++;
}
Return strlen;
}
2. Only Chinese characters can be entered using regular expressions
The Code is as follows:
I hope this article will help you design javascript programs.