<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.jzread.com/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "" <html xmlns= "http://www.jzread.com/1999/xhtml" <head> < Meta http-equiv= "Content-type" content= "HTML; Charset=utf-8 "/> <style type=" text/css "media=" screen " body{margin:0px; div{width:400px; margin:0px Auto; Div h3{background: #CCCCCC; font-size:14px; font-weight:bold; text-align:center; line-height:22px; margin:6px 0px; padding:0px; Div textarea{width:388px height:120px border: #999999 1px solid; margin:0px; padding:0px; font-size:12px; padding : 5px; line-height:16px; Div p{font-size:12px; margin:0px; line-height:22px;} Div p span{color: #FF6600; font-weight:bold;} </style> <title>javascript Limit the maximum number of characters entered in the text box </title> </head>
<body> <div> <h3> character test </h3> <textarea rows= "cols=" Checkmaxlength (this,20) "onpaste=" Checkmaxlength (this,20) ></textarea> <p> Max <span>20 </span> characters, one character 2 characters. </p> </div>
<script type= "Text/javascript" function checkmaxlength (area, maxLength) { var currentstr= ""; for (var i=0,len=area.value.length;i<len;i++) { Currentstr+=area.value.charat (i); if (getcharlength (CURRENTSTR) >maxlength) { Area.value=area.value.substr (0,i); return; } } }
function Getcharlength (str) { var charlen=0; for (Var i=0,len=str.length;i<len;i++) { if (Str.charcodeat (i) >255) { charlen+=2; }else{ Charlen+=1; } } return Charlen; } </script> </body> </html>
Programme III
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.jzread.com/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "" <html xmlns= "http://www.jzread.com/1999/xhtml" <head> < Meta http-equiv= "Content-type" content= "HTML; Charset=utf-8 "/> <style type=" text/css "media=" screen " body{margin:0px; div{width:400px; margin:0px Auto; Div h3{background: #CCCCCC; font-size:14px; font-weight:bold; text-align:center; line-height:22px; margin:6px 0px; padding:0px; Div textarea{width:388px height:120px border: #999999 1px solid; margin:0px; padding:0px; font-size:12px; padding : 5px; line-height:16px; Div p{font-size:12px; margin:0px; line-height:22px;} Div p span{color: #FF6600; font-weight:bold;} </style> <title>javascript Limit the maximum number of characters entered in the text box </title> </head>
<body> <div> <h3> character test </h3> <textarea rows= "cols=" Checkmaxlength (this,20) "onpaste=" return false; ></textarea> <p> Max <span>20</span> characters, one kanji 2 characters, you can also enter <span>20</span> Character </p> </div>
<script type= "Text/javascript" var s=document.getelementsbytagname ("span") [1]; function Checkmaxlength (area, maxLength) { var currentstr= ""; if (area.value.length==0) {s.innerhtml=maxlength} for (var i=0,len=area.value.length;i<len;i++) { Currentstr+=area.value.charat (i); if (getcharlength (CURRENTSTR) >maxlength) { Area.value=area.value.substr (0,i); return; }else{ S.innerhtml=maxlength-getcharlengtH (CURRENTSTR); } } }
Function Getcharlength (str) { var charlen=0 for (Var i=0,len=str.length;i <len;i++) { if (str.charcodeat (i) >255) { charlen+=2; }else{ charlen+=1 } } return Charlen } </script> </body> </html>
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.