Javascript restricts the user to typing Chinese characters
- 2020-03-30 04:21:09
- OfStack
This article is an example of how javascript can restrict users to typing Chinese characters. Share 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, then the string length plus 2, if the regular we directly use \\u4E00-\\u9FA5 can solve.
1. Unicode tests Chinese characters
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 the string length plus 2
strlen += 2;
else
strlen++;
}
return strlen;
}