多行文本框指定最大长度 [英] Specifying maxlength for multiline textbox

查看:164
本文介绍了多行文本框指定最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用asp:

 < ASP:文本框ID =txtInput=服务器的TextMode =多行>< / ASP:文本框>

和我想的方式来指定MaxLength属性,但显然没有这样多行文本框。我一直在尝试使用一些JavaScript的安其preSS事件
安其preSS =返回textboxMultilineMaxNumber(这一点,最大长度)

 函数textboxMultilineMaxNumber(TXT,MAXLEN){
            尝试{
                如果(txt.value.length>(MAXLEN - 1))返回false;            }赶上(E){
            }
            返回true;
        }

而这个javascript函数工作正常,问题是,写入字符后,它不允许你删除并替换其中任何一个,这样的行为是不希望的。

你有任何想法,我还能在上面code更改,以避免或任何其他方式来避开它。


解决方案

 函数checkTextAreaMaxLength(文本框中即长度)
{        VAR MLEN = textBox中[最大长度];
        如果(空== MLEN)
            MLEN =长度;        VAR最大长度= parseInt函数(MLEN);
        如果(!checkSpecialKeys(E))
        {
         如果(textBox.value.length>最大长度-1)
         {
            如果(window.event)//即
              e.returnValue = FALSE;
            否则//火狐
                亦即preventDefault();
         }
    }
}
功能checkSpecialKeys(五)
{
    如果(e.key code = 8安培;!&安培; e.key code = 46安培;!&安培;!e.key code = 37安培;&安培; e.key $ C $ !C = 38安培;&安培; e.key code = 39安培;!&安培;!e.key code = 40)
        返回false;
    其他
        返回true;
}

在控制调用它是这样的:

 < ASP:文本框行数=5列=80ID =txtCommentsForSearchMAXLENGTH ='1999'的的onkeydown =checkTextAreaMaxLength(这一点,事件,'1999'的) ;的TextMode =多行=服务器> < / ASP:文本框>

您也可以只使用checkSpecialKeys功能来验证你的JavaScript实现输入。

I'm trying to use asp:

<asp:TextBox ID="txtInput" runat="server" TextMode="MultiLine"></asp:TextBox>

and i want a way to specify the maxlength property ,but apparently there is not such for multiline textbox. I've been trying to use some javascript for the onkeypress event onkeypress="return textboxMultilineMaxNumber(this,maxlength)"

function textboxMultilineMaxNumber(txt, maxLen) {
            try {
                if (txt.value.length > (maxLen - 1)) return false;

            } catch (e) {
            }
            return true;
        }

while working fine the problem with this javascript function is that after writing characters it doesn't allow you to delete and substitute any of them, such a behavior is not desired.

Have you got any idea what can i possibly change in the above code to avoid that or any other ways to get round it.

解决方案

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.

这篇关于多行文本框指定最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆