TextArea does not support maxlength (jquery) _ jquery

Source: Internet
Author: User
Write a jquery extension, so that you can easily control the maximum length of textarea. Use. net Control for a long time, when adding control length control to the TextBox of a page, simply add a maxlength = 'xxx', but the test is always not good, the reason is that the multiline mode is set. In this case, the generated html code is textarea, And the maxlength attribute is not added because IE does not support the maxlength attribute of textarea. Therefore, in firefox 6, we found that firefox supports this attribute. That's easy. Write a jquery extension, so that you can easily control the maximum length of textarea.
The extension code is as follows:

The Code is as follows:


(Function ($ ){
$. Fn. textarealimit = function (settings ){
Settings = jQuery. extend ({
Length: 1000
}, Settings );
MaxLength = settings. length;
$ (This ). attr ("maxlength", maxLength ). bind ("keydown", doKeydown ). bind ("keypress", doKeypress ). bind ("beforepaste", doBeforePaste ). bind ("paste", doPaste );
Function doKeypress ()
{
Var oTR = document. selection. createRange ()
If (oTR. text. length> = 1)
Event. returnValue = true
Else if (this. value. length> maxLength-1)
Event. returnValue = false
}
Function doKeydown ()
{
Var _ obj = this;
SetTimeout (function ()
{
If (_ obj. value. length> maxLength-1)
{
Var oTR = too many Doc ument. selection. createRange ()
OTR. moveStart ("character",-1 * (_ obj. value. length-maxLength ))
OTR. text = ""
}
}, 1)
}
Function doBeforePaste ()
{
Event. returnValue = false
}
Function doPaste ()
{
Event. returnValue = false
Var oTR = document. selection. createRange ()
Var iInsertLength = maxLength-this. value. length + oTR. text. length
Var sData = window. clipboardData. getData ("Text"). substr (0, iInsertLength)
OTR. text = sData;
}
}
}) (JQuery );


The above is only for IE to control the copy and paste control and the input control, due to the use of IE features, these methods are invalid in firefox.
Call code:

The Code is as follows:


$ (Document). ready (function (){
$ ("# Ctl00_ZiiOContent_ucQuestionnaire_txtquestion4_2"). textarealimit ();
});

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.