This article illustrates the method of javascript restricting users from entering Chinese characters only. Share it for your reference. The specific implementation method is as follows:
When verifying the function, we must understand that if it is a Chinese character, the length of the string is added by 2. If it is a regular, we can directly use //u4E00-//u9FA5 to solve it.
1. Unicode test Chinese characters
The code copy 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 string length
strlen += 2;
else
strlen++;
}
return strlen;
}
2. Only Chinese characters can be entered using regular rules
Copy the code code as follows: <input onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))">
I hope this article will be helpful to everyone's JavaScript programming.