Home > Article > Web Front-end > A simple example of js intercepting fixed-length Chinese and English characters_javascript skills
Although js provides a function for calculating the number of bytes occupied by a string, it cannot correctly calculate the number of bytes occupied by Chinese characters, such as document.form1.username.value="I am Chinese",document.form1.username. value.length returns 5 instead of 10, which brings some troubles to programming, such as limiting the input of fixed-length characters and intercepting fixed-length strings. Because of this problem, the expected effect cannot be achieved. The following is what I wrote. A simple function, used to intercept fixed-length strings, applicable to both Chinese and English. If there is anything wrong, please correct me!