Javascript 当超过文本框限制时,直观地向用户指示

Javascript 当超过文本框限制时,直观地向用户指示,javascript,jquery,asp.net,ajax,Javascript,Jquery,Asp.net,Ajax,我是新的asp.net,现在我需要直观地向用户指示何时超过文本框限制。请给出一个解决方案 <asp:TextBox ID="Txtbox" runat="server" Height="50px" TextMode="MultiLine" Width="150px" MaxLength="50"></asp:TextBox> 在文本框中,添加以下内容 onkeypress="return CheckLength(this.value,50);" jav

我是新的asp.net,现在我需要直观地向用户指示何时超过文本框限制。请给出一个解决方案

<asp:TextBox ID="Txtbox" runat="server" Height="50px" TextMode="MultiLine"
        Width="150px" MaxLength="50"></asp:TextBox>

在文本框中,添加以下内容

onkeypress="return CheckLength(this.value,50);"
javascript函数
CheckLength

function CheckLength(ctrl, maxlen) {
    var textbox = ctrl;
    if (textbox.length >= maxlen) {
        return false;
    }
    else {
        return true;
    }
}
这将阻止您在文本框中输入超过50个字符。也许可以在文本框旁边添加工具提示

<asp:Label ID="lblMark1" runat="server" Text="You can enter 50 characters only." Font-Size="0.65em"></asp:Label>

您可以在
JQuery的帮助下实现这一点,因此在文本中使用type时,它会显示剩余的字符数

试试这个

function CheckTextLength(text, long) {
    var maxlength = new Number(long); // Change number to your max length.
    if (text.value.length > maxlength) {
        text.value = text.value.substring(0, maxlength);
        $('#YourErrorDivid').empty().append(" Only " + long + " characters allowed");
    }
}

 <asp:TextBox runat="server" ID="Txtbox" Width="450px" TextMode="MultiLine"
            onKeyPress="CheckTextLength(this,50)"></asp:TextBox>
$("#Txtbox").on('input',function () {
    if($(this).val().length == $(this).attr('maxlength')) {
        $(this).css('background-color','red');
    }
});
函数CheckTextLength(文本,长){
var maxlength=新数字(长);//将数字更改为最大长度。
如果(text.value.length>maxlength){
text.value=text.value.substring(0,maxlength);
$('#YourErrorDivid').empty().append(“仅允许“+long+”字符”);
}
}
试试这个

function CheckTextLength(text, long) {
    var maxlength = new Number(long); // Change number to your max length.
    if (text.value.length > maxlength) {
        text.value = text.value.substring(0, maxlength);
        $('#YourErrorDivid').empty().append(" Only " + long + " characters allowed");
    }
}

 <asp:TextBox runat="server" ID="Txtbox" Width="450px" TextMode="MultiLine"
            onKeyPress="CheckTextLength(this,50)"></asp:TextBox>
$("#Txtbox").on('input',function () {
    if($(this).val().length == $(this).attr('maxlength')) {
        $(this).css('background-color','red');
    }
});
这是一个

显示轮廓,因为它不会占用任何空间。如果用户删除了一些字符,我也会删除大纲

$("#txtName").keyup(function(){
    if($(this).val().length >= $(this).attr('maxLength'))
    {
        $(this).css('outline','2px solid red');
    }
    else 
    {
        $(this).css('outline','');
    }
});
$("#txtName").keyup(function(){
    if($(this).val().length >= $(this).attr('maxLength'))
    {
        $(this).css('outline','2px solid red');
    }
    else 
    {
        $(this).css('outline','');
    }
});