Javascript-form character length and database Field Length

Source: Internet
Author: User
The length of an html form is calculated based on the number of characters, whether it is a Chinese character or a letter, but the database is calculated by byte. The number of Chinese characters occupies 1 of 2 letters, in this way, the length of data written exceeds the limit. Two questions: 1. is there a good way to go to the front-end or php or... the length of an html form is calculated based on the number of characters, whether it is a Chinese character or a letter, but the database is calculated by byte. The number of Chinese characters occupies 1 of 2 letters, in this way, the length of data written exceeds the limit.

Two problems:
1. Is there a good way to calculate the length using a uniform method between the front-end, php or mysql?
2. Can I only set the database length to be large enough so that the form will never exceed?

.

Reply content:

The length of an html form is calculated based on the number of characters, whether it is a Chinese character or a letter, but the database is calculated by byte. The number of Chinese characters occupies 1 of 2 letters, in this way, the length of data written exceeds the limit.

Two problems:
1. Is there a good way to calculate the length using a uniform method between the front-end, php or mysql?
2. Can I only set the database length to be large enough so that the form will never exceed?

.

After mysql 4.1, char and varchar define the character length.
For more information, see The CHAR and VARCHAR Types.
As mentioned in document 4.1.

The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. for example, CHAR (30) can hold up to 30 characters. (Before MySQL 4.1, the length is interpreted as number of bytes .)

The uniform length calculation is based on the concept of "length". First, you must specify what your "length" refers.
If "length" refers to the byte length, you can (assuming the page encoding is utf8 ):

// javascriptfunction lengthInUtf8Bytes(str) {  // Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.  var m = encodeURIComponent(str).match(/%[89ABab]/g);  return str.length + (m ? m.length : 0);}len = lengthInUtf8Bytes(str);//php$len = strlen($str);//mysqlCREATE DATABASE `byte_test`    CHARACTER SET latin1     COLLATE latin1_general_ci;

The javascript function lengthInUtf8Bytes is from String length in bytes in JavaScript
If "length" refers to the encoding length, it is assumed that the page is utf8 encoded:

// javascriptlen = str.length;// php$len = mb_strlen($str,"UTF-8");// mysqlCREATE DATABASE `utf8_test`    CHARACTER SET utf8    COLLATE utf8_general_ci;

In this way, the length is consistent, so it is important to define your "length" at the very beginning.

Front-end restrictions are just a blind eye. It takes a long time for databases to compete.

If you all use UTF-8 encoding, there is no such annoyance. Database table with UTF-8, a Chinese character or letter is one, that is to say varchar (2) type, you can certainly save the word "test. The actual storage of mysql uses 6 bytes. Generally, the next Chinese character needs to be stored in 3 bytes. However, this is what mysql does at the underlying layer, and it has nothing to do with the length of the varchar defined by you.

Excellent professionalism on the second floor, like

The database fields do not have to be so precise. The ten bytes must be limited to ten bytes. The maximum length is implemented by the program. The database fields only provide a range.

If the parameter length can be verified in the backend, others can directly request the backend service interface.
The front-end can control the input length of maxlength.

If you are not afraid of trouble,
You can design a universal interceptor for pre-submission check,
First, obtain the metadata information of the database, such as encoding and field length (this information that can be cached will not change ).
Then, based on this calculation, the maximum length of the corresponding field can contain several Chinese characters and several English characters.
Then calculate the corresponding number of bytes in the front-end or php, so there will be no problem.
However, the performance may be slightly compromised.
A better way is to use variable-length fields for data with an indefinite length. For example, varchar is designed to be large enough.

Related Article

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.