Js verification character length examples

Source: Internet
Author: User

In js, we can use the length function to determine the length of common pure letters and numbers. However, we may not be very accurate about Chinese characters, below I will add some related instances to your favorites.

For example, use the length function.

The Code is as follows: Copy code


<Script type = "text/javascript">

Var txt = "Don't worry, n will have it. Come on! Hope to be ahead"
Document. write (txt. length)

</Script>

The output result of the above Code is: 19


Two Chinese Characters

The Code is as follows: Copy code

// Oracle Varchar2 corresponds to three bytes for a Chinese character, so it is replaced by three x bytes.
Var commentValue = commentValue. replace (/[^ x00-xff]/g, "xxx ");
If (commentValue. length> 500 ){
Alert ("Max length is 150 Chinese or 500 English ");
Return false;
}

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.