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.