JS limits the length of text box input (supports character restrictions)

Source: Internet
Author: User

Features/features:

1. display the number of words that can be entered in real time (in bytes)

2. Two restrictions (length and number of bytes)

3. The Chinese input method can be used normally without any bugs.

4. The same page can be used multiple times without interfering with each other

Limit. js

Function Limit () {<br/> var txtnote; // text box <br/> var txtlimit; // input indicating the number of words <br/> var limitcount; // limit the number of words <br/> var isbyte; // whether to use the byte length limit (1 Chinese Character = 2 characters) <br/> var txtlength; // when the limit is reached, string Length <br/> var txtbyte; <br/> This. init = function () {<br/> txtnote=this.txt note; <br/> txtlimit#this.txt limit; <br/> limitcount = This. limitcount; <br/> isbyte = This. isbyte; <br/> txtnote. onkeydown = function () {wordslimit ()}; txtnote. onkeyup = function () {wordslimit () }; <br/> txtlimit. value = limitcount; <br/>}< br/> function wordslimit () {<br/> var notecount = 0; <br/> If (isbyte) {notecount = txtnote. value. replace (/[^/x00-/xFF]/g, "XX "). length} else {notecount = txtnote. value. length} <br/> If (notecount> limitcount) {<br/> If (isbyte) {<br/> txtnote. value = txtnote. value. substring (0, txtlength + math. floor (limitcount-txtbyte)/2); <br/> txtbyte = txtnote. value. replace (/[^/x00-/xFF]/g, "XX "). length; <br/> txtlimit. value = limitcount-txtbyte; <br/>}else {<br/> txtnote. value = txtnote. value. substring (0, limitcount); <br/> txtlimit. value = 0; <br/>}< br/>} else {<br/> txtlimit. value = limitcount-notecount; <br/>}< br/> txtlength = txtnote. value. length; // record the length after each input <br/> txtbyte = txtnote. value. replace (/[^/x00-/xFF]/g, "XX "). length; <br/>}< br/>}

Page call:

<HTML> <br/> <body> <br/> <input id = "txtnote"/> <br/> You can also enter <input type = "text" id =" txtcount "/> characters <br/> </body> <br/> <MCE: script Type = "text/JavaScript"> <! -- <Br/> var Lim = new limit (); <br/> lim.txt note = document. getelementbyid ("txtnote"); <br/> lim.txt Limit = document. getelementbyid ("txtcount"); <br/> Lim. limitcount = 20; <br/> Lim. isbyte = true; <br/> Lim. init (); <br/> // --> </MCE: SCRIPT> <br/> </ptml>

 

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.