Solve the problem that textarea does not recognize the maxlength attribute in IE.

Source: Internet
Author: User

First, let's look at a simple solution. The Code is as follows:

$ ("Textarea [maxlength]"). keyup (function () {var area = $ (this); var max = parseint (area. ATTR ("maxlength"), 10); // obtain the value of maxlength if (max> 0) {If (area. val (). length> MAX) {// The text length of textarea is greater than that of maxlength area. val (area. val (). substr (0, max); // truncate the text of textarea and re-assign the value }}); // handle the copied characters $ ("textarea [maxlength]"). blur (function () {var area = $ (this); var max = parseint (area. ATTR ("maxlength"), 10); // obtain the value of maxlength if (max> 0) {If (area. val (). length> MAX) {// The text length of textarea is greater than that of maxlength area. val (area. val (). substr (0, max); // truncate the text of textarea and assign a new value }}});

The code is very simple, but the experience is not good, that is, you will see that the characters in textarea have been input, and then the characters are truncated. This experience is obviously the effect of setting the maxlength attribute in input (experience) different, but it can also be used together. The following describes more complex implementations. The following implementation provides the same experience as setting maxlength in input.

Function onmyinput (O, maxlength) {If (O. value. length> = maxlength) {If (O. value. length> maxlength) O. value = O. value. substring (0, maxlength); Return false;} return true;} function mygetclipdata () {If (! Document. all) {Netscape. security. privilegemanager. enableprivilege ('universalxpconnect '); var clip = components. classes ['@ mozilla.org/widget/clipboard1_1').createinstance (components. interfaces. nsiclipboard); var trans = components. classes ['@ mozilla.org/widget/transferable1_1').createinstance (components. interfaces. nsitransferable); Trans. adddataflavor ('text/Unicode '); var STR = components. classes ["@ Mo Zilla.org/supports-string1_1 "]. createinstance (components. interfaces. nsisupportsstring); clip. getdata (trans, Clip. kglobalclipboard); var STR = new object (); var strlength = new object (); Trans. gettransferdata ("text/Unicode", STR, strlength); If (STR) STR = Str. value. queryInterface (components. interfaces. nsisupportsstring); var pastetext; If (STR) pastetext = Str. data. substring (0, strlength. value/2); Return pastetext;} else {return window. clipboardData. getdata ("text") ;}} function mysetclipdata (o) {If (! Document. all) {Netscape. security. privilegemanager. enableprivilege ('universalxpconnect '); var clip = components. classes ['@ mozilla.org/widget/clipboard1_1').createinstance (components. interfaces. nsiclipboard); var trans = components. classes ['@ mozilla.org/widget/transferable1_1').createinstance (components. interfaces. nsitransferable); Trans. adddataflavor ("text/Unicode"); var STR = components. classes ["@ Mo Zilla.org/supports-string1_1 "]. createinstance (components. interfaces. nsisupportsstring); Str. data = O; trans. settransferdata ("text/Unicode", STR, O. length * 2); var clipid = components. interfaces. nsiclipboard; clip. setdata (trans, null, clipid. kglobalclipboard);} else {window. clipboardData. setdata ("text", O) ;}} function onmypaste (O, maxlength) {var nmaxlen = O. getattribute? Parseint (maxlength): ""; if (! Document. all) {// alert ("unexecutable Code");} else {If (document. selection. createRange (). text. length> 0) {var ovalueandclipboarddata = O. value + window. clipboardData. getdata ("text"); If (O. getattribute & ovalueandclipboarddata. length-document. selection. createRange (). text. length> nmaxlen) {If (window. clipboardData. getdata ("text "). substring (0, document. selection. createRange (). text. length + nmaxlen- O. value. Length )! = "") Window. clipboardData. setdata ("text", window. clipboardData. getdata ("text "). substring (0, document. selection. createRange (). text. length + nmaxlen-o. value. length); else return false ;}} else {var ovalueandclipboarddata = O. value + window. clipboardData. getdata ("text"); If (O. getattribute & ovalueandclipboarddata. length> nmaxlen) {If (ovalueandclipboarddata. substring (0, nmaxlen-o. value. len Success )! = "") Window. clipboardData. setdata ("text", ovalueandclipboarddata. substring (0, nmaxlen-o. value. length); else return false ;}} return true ;}} function onmykeypress (O, maxlength) {If (! Document. All) {var nmaxlen = O. getattribute? Parseint (maxlength): ""; if (onmykeypress. caller. arguments [0]. ctrlkey = true) {If (onmykeypress. caller. arguments [0]. which = 118) {If (O. selectionstart <O. selectionend) {var ovalueandclipboarddata = O. value + mygetclipdata (); If (O. getattribute & (ovalueandclipboarddata. length-o. selectionend + O. selectionstart> nmaxlen) {If (mygetclipdata (). substring (0, O. selectionend-o. selectionstar T + nmaxlen-o. value. Length )! = "") Mysetclipdata (mygetclipdata (). substring (0, O. selectionend-o. selectionstart + nmaxlen-o. value. length); else return false ;}} else {var ovalueandclipboarddata = O. value + mygetclipdata (); If (O. getattribute & ovalueandclipboarddata. length> nmaxlen) {If (ovalueandclipboarddata. substring (0, nmaxlen-o. value. length )! = "") Mysetclipdata (ovalueandclipboarddata. substring (0, nmaxlen-o. value. length); else return false;} return true;} If (onmykeypress. caller. arguments [0]. which = 0 | onmykeypress. caller. arguments [0]. which = 8) return true; If (O. value. length> = maxlength) {If (O. selectionstart <O. selectionend) return true; If (O. value. length> maxlength) O. value = O. value. substring (0, maxlength); Return False;} else return true;} else {If (document. selection. createRange (). text. length> 0) return true; If (O. value. length >=maxlength) return false; else return true ;}} window. onload = init_maxlength; function init_maxlength () {var textareaobj = document. getelementsbytagname ("textarea"); var maxlength; For (VAR I = 0; I <textareaobj. length; I ++) {maxlength = textareaobj [I]. getattribute ("maxlen Required ") = NULL? 0: textareaobj [I]. getattribute ("maxlength"); If (maxlength = 0) continue; textareaobj [I]. onpaste = function () {return onmypaste (this, maxlength)}; textareaobj [I]. onkeypress = function () {return onmykeypress (this, maxlength)}; textareaobj [I]. onpropertychange = function () {onmyinput (this, maxlength)}; If (! Document. All) {textareaobj [I]. setattribute ("oninput", "Return onmyinput (this," + maxlength + ")");}}}

 

Solve the problem that textarea does not recognize the maxlength attribute in IE.

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.