Определение maxlength для многострочного текстового поля

Согласно msdn:Performance Подсказки и Приемы , который можно использовать, пытаются поймать без любой проблемы производительности до реального occure броска.

75
задан Barry Michael Doyle 18 August 2016 в 10:57
поделиться

2 ответа

try this javascript:

function checkTextAreaMaxLength(textBox,e, length)
{

        var mLen = textBox["MaxLength"];
        if(null==mLen)
            mLen=length;

        var maxLength = parseInt(mLen);
        if(!checkSpecialKeys(e))
        {
         if(textBox.value.length > maxLength-1)
         {
            if(window.event)//IE
              e.returnValue = false;
            else//Firefox
                e.preventDefault();
         }
    }   
}
function checkSpecialKeys(e)
{
    if(e.keyCode !=8 && e.keyCode!=46 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
        return false;
    else
        return true;
}

On the control invoke it like this:

<asp:TextBox Rows="5" Columns="80" ID="txtCommentsForSearch" MaxLength='1999' onkeyDown="checkTextAreaMaxLength(this,event,'1999');"  TextMode="multiLine" runat="server"> </asp:TextBox>

You could also just use the checkSpecialKeys function to validate the input on your javascript implementation.

40
ответ дан 24 November 2019 в 11:32
поделиться

Have a look at this. The only way to solve it is by javascript as you tried.

EDIT: Try changing the event to keypressup.

1
ответ дан 24 November 2019 в 11:32
поделиться
Другие вопросы по тегам:

Похожие вопросы: