Commonly used JavaScript limit input character number code

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax code content function html http javascript media

<! 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 Tutorial" 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 number Test </h3>
<textarea rows= "cols=" onkeypress= "return Checkmaxlength (this,20)" ></textarea>
<p> Max <span>20</span> characters. </p>
</div>

<script type= "Text/javascript Tutorial" >
function Checkmaxlength (otextarea,maxlength) {
return otextarea.value.length!=maxlength;
}
</script>
</body>
</html>

Programme II

<! 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 number Test </h3>
<textarea rows= "cols=" onkeyup= "Checkmaxlength (this,20)" onpaste= "Checkmaxlength (this,20)" ></ Textarea>
<p> Max <span>20</span> characters. </p>
</div>

<script type= "Text/javascript" >
function Checkmaxlength (area, maxLength) {
if (Area.value.length >maxlength) {
Area.value = area.value.substr (0, maxLength);
}
}
</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=" 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>
 

Related Article

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.