JS to determine the number of string bytes and intercept the length of the method _javascript tips

Source: Internet
Author: User

This article illustrates the method of JS to judge the number of string bytes and intercept the length. Share to everyone for your reference, specific as follows:

This is in the project production, accumulated to a thing, feel the effect can also now put on the effect:

So, on the page, we need to detect two things, one is the number of bytes, one is the number of characters.

Because the database, requires the length of the title of 200 bytes, then the specific JS code is as follows:

/************************************************************************* * CodeBy: Scy codedate:2011 Year March 11 12:01:16 * DESC: Mainly used to determine the number of bytes currently entered in order to limit the length of the input caption function *******************************************
/var matchwords; function Notifytextlength () {var inputnum = document.getElementById ("Txttitle"). Value.replace (/[^\x00-\xff]/g, "* *") . length;
    Gets the number of bytes entered if (Inputnum <=) {matchwords = document.getElementById ("Txttitle"). Value.length;
    document.getElementById ("Inputedword"). InnerHTML = Inputnum + "byte," + Matchwords + "character"; document.getElementById ("Inputtingword"). InnerHTML = (200-inputnum) + "letter," + (Math.Round (((200-inputnum)/2)-0.5)) + "
  Chinese characters "; } if (Inputnum >) {document.getElementById ("Txttitle"). Value = document.getElementById ("Txttitle"). Value.s Ubstring (0, matchwords); If more than 200 bytes are intercepted, 200 bytes}} 

Where matchwords represents the number of characters that are matched when the number of bytes is less than 200, and Inputnum is the number of bytes entered.

When the header enters more than 200 bytes, it is intercepted by the number of characters.

The HTML code is as follows:

<input id= "Txttitle" type= "text" class= "Inputtext" runat= "Server" onpropertychange= "Notifytextlength ();"/>
you have currently entered <span id= "Inputedword" style= "color:red" ></span>
can also enter <span id= "Inputtingword" style = "color:red;" ></span>

More readers interested in JavaScript-related content can view the site topics: "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and Skills summary", "JavaScript Error and debugging skills summary", " JavaScript data structure and algorithm skills summary, "javascript traversal algorithm and Skills summary" and "JavaScript Mathematical Computing Usage Summary"

I hope this article will help you with JavaScript programming.

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.