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>