Limit.js Code
Copy Code code as follows:
txt: text box jquery object
Limit: Limit the number of words
Isbyte:true: Limit as number of bytes; False: View limit as number of characters
CB: Callback function, parameter is the number of words can be entered
function Initlimit (TXT,LIMIT,ISBYTE,CB) {
Txt.keyup (function () {
var str=txt.val ();
var Charlen;
var bytelen=0;
if (isbyte) {//original blog: Blog.csdn.net/bluceyoung
for (Var i=0;i<str.length;i++) {
if (Str.charcodeat (i) >255) {
bytelen+=2;
}else{
bytelen++;
}
}
Charlen = Math.floor ((limit-bytelen)/2);
}else{
Bytelen=str.length;
Charlen=limit-bytelen;
}
CB (Charlen);
});
}
page Code:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta content= "text/html; Charset=utf-8 "http-equiv=" Content-type "/>
<script src= "Http://code.jquery.com/jquery-1.8.2.min.js" type= "Text/javascript" >
</script>
<script type= "Text/javascript" src= "Limit.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
Initlimit ($ ("#txt"), 10,true,function (c) {
if (c>=0) {
$ ("#show"). Val ("Can also enter" the word "+c+");
}else{
$ ("#show"). Val ("Already Over" + (c) + "word");
}
});
Initlimit ($ ("#txt1"), 10,true,function (c) {
if (c>=0) {
$ ("#show1"). Val ("Can also enter" the word "+c+");
}else{
$ ("#show1"). Val ("Already Over" + (c) + "word");
}
});
});
</script>
<body>
<input type= "text" id= "TXT"/><input id= "show" type= "text"/><br/>
<input type= "text" id= "txt1"/><input id= "Show1" type= "text"/>
</body>