JavaScript textarea Word limit _ form effects

Source: Internet
Author: User
This one is to read the value of the TextArea MaxLength property, to calculate, the idea is very good, but the modification of the trouble Ah, but for JS enthusiasts is a good reference, by reading the property value to control.
<textarea id= "runcode18816" > <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" > <ptml> <pead> ; <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/> <title>textarea word limit Practice code more have reference network by ahuinan</title> <script language= "JavaScript" type= "Text/javascript" > var textutil = new Object (); Textutil.notmax = function (Otextarea) {var maxtext = Otextarea.getattribute ("maxlength"); if (OTextArea.value.length > Maxtext) {otextarea.value = oTextArea.value.substring (0,maxtext ); Alert ("Beyond Limits"); } </script> </pead> <body> <textarea rows= "cols=" O Npropertychange= "Textutil.notmax (This)" Maxlength= "3" ></textarea> </body> </ptml> </textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The following code, which directly calculates the length of the document
<textarea id="runcode10530"><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead runat=" Server "> <title>input word Limit </ title> <script type= "Text/javascript" language= "JavaScript" > Function Chck () {var len = 0; var Str=document.getelementbyid ("Input1"). Value; for (var i=0 i<str.length; i++) {if (Str.charcodeat (i) >127 | | str.charcodeat (i) ==94) {len + 2; else {len + +; } alert (len); if (len>20) {alert ("Sorry, not more than 20 bytes, Chinese count two!") "); } </script> </pead> <body> <form id= "Form1" > <div> <input id= "INPUT1" Type= "Tex T "onblur=" chck (); "/> <input type=" button "value=" Calculates "onclick=" Chck (); "/> </div> </form> </b Ody> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The following code is a wide range of applications on the Web, with the remaining word tip effect code, recommended
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <meta name=" robots "content=" all "/> <title>js limit textarea input box input word </title> < SCRIPT language= "JavaScript" > <!--function checktext (text) {allvalid = true; for (i = 0; i < text.length i++) {if (Text.charat (i)!= "") {Allvalid = false; Break } return allvalid; function Gbcount (message,total,used,remain) {var max; max = Total.value; if (Message.value.length > Max) {message.value = message.value.substring (0,max); Used.value = max; Remain.value = 0; Alert ("Message not more than 200 words!"); else {used.value = message.value.length; Remain.value = Max-used.value; }--> </script> <style type= "Text/css" > <!--* {padding:0; margin:0;} BodyHTML {text-align:left; font-size:12px; line-height:150%; margin:0 auto; background: #fff; padding-top:20px;} fieldset {padding:10px; width:550px; margin:0 Auto;} Legend {font-size:14px; font-weight:bold;} . inputtext {border:none; background: #fff;} --> </style> </pead> <body> <form action= "savedata.asp" method= "POST" onsubmit= "return Datache CK (); " > <fieldset><legend> Please enter the content </legend> <textarea name= "Memo" rows= "ten" wrap=physical cols= "75" Onkeydown= "Gbcount (This.form.memo,this.form.total,this.form.used,this.form.remain);" OnKeyUp= "Gbcount ( This.form.memo,this.form.total,this.form.used,this.form.remain); " ></textarea> <p> maximum words: <input disabled maxlength= "4" name= "Total" size= "3" value= "" class= " Inputtext "> already used words: <input disabled maxlength=" 4 "name=" used "size=" 3 "value=" 0 "class=" inputtext "> Remaining words: <i Nput disabled maxlength= "4" name= "Remain" size= "3" value= "" class= "Inputtext" ></td> </p> </fieldset> </form> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

For more calculation of string length code, refer to this article.
Http://www.jb51.net/article/21343.htm

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.