JS limit the number of bytes in the input box, two bytes in Chinese, one byte in English letters __JS

Source: Internet
Author: User

Maybe there are a lot of similar articles on the Internet, but here you should know, JS inside, Chinese and letter length is 1, but the database does not know Ah, a lot of database are in accordance with the byte, so, control the number of bytes in the input box seems to be more important than the length limit.

Ask a question:

1, the Limit text box is 16 bytes, only input Chinese and letters

Solution Idea:

1, the judgment can only be Chinese and English

2, the judge length can only be 16

3, there is a non conformance, will enter the loop:

The first step: Replace all the #, because I want to use this special character later

Step two: Replace all Chinese with: @@

Step three: Intercept the 0-20 byte length, where the Chinese has been turned into two lengths

Step Fourth: Convert all @@ 转换 #

Fifth step: Replace the last possible Chinese half byte, a @

Step sixth: Get the last remaining character length, which is the length of the string to intercept


<!
    DOCTYPE html>  



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.