JS limits the number of input bytes in the input box

Source: Internet
Author: User

See the results:

 

  • You can enter 5 bytes:
  • You can enter 6 bytes:
  • You can enter 7 bytes:
  • You can enter n Bytes:

The Code is as follows: (I encapsulated the code based on the prototype and found that the Code is being added and changed again)

 

Thanks for sparks345's question about pasting.

In this case, you can add only several events onpropertychange, oninput, obj. Watch ("value", function (ID, oval, nval ){})

For details, see here to track the compatibility of input value changes.

 

Code here

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> limit the number of input bytes </title>
<Meta name = "author" content = "sohighthesky"/>
<Style type = "text/CSS">
# Div1 {width: 500px; margin: 20px auto ;}
# Div1 ul {list-style-type: decimal; line-Height: 25px ;}
</Style>
</Head>
<Body>
<Div id = "div1">
<Ul>
<Li> you can enter five Bytes: <input type = "text" id = "txt1" value = "Hello e"/> </LI>
<Li> you can enter 6 bytes: <input type = "text" id = "txt2" value = "hello,"/> </LI>
<Li> you can enter seven Bytes: <input type = "text" id = "txt3" value = ", he"/> </LI>
<Li> n Bytes can be entered: <input type = "text" id = "txt4"/> </LI>
</Ul>
</Div>
</Body>
<SCRIPT type = "text/JavaScript">
/*!
* Author: sohighthesky
* Date: 2010-1-16
*/
! Function (){
VaR Len = function (s) {// get the string's Byte Length
S = string (s );
Return S. Length + (S. Match (/[^ \ x00-\ xFF]/G) | ""). length; // Add the length of the matched fullwidth characters
},
Limitdo = function (Limit ){
Var val = This. value;
If (LEN (VAL)> limit ){
// Val = Val. substr (0, limit );
While (LEN (val = Val. substr (0, Val. Length-1)> limit );
This. value = val;
}
}, $ = Function (ID) {return typeof (ID) === 'string '? Document. getelementbyid (ID): Id ;};

$ ("Txt1"). onkeyup = function () {limitdo. Call (this, 5 )};
$ ("Txt2"). onkeyup = function () {limitdo. Call (this, 6 )};
$ ("Txt3"). onkeyup = function () {limitdo. Call (this, 7 )};
}();
</SCRIPT>
</Html>

 

 

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.