JavaScript中的文本框最小长度验证

时间:2017-05-10 06:43:11

标签: javascript c# jquery asp.net

总计数100.在我输入101个字母显示警告或错误消息后,它在此代码中无效。

HTML

$707.17

JavaScript

<asp:TextBox ID="txtarea" runat="server" Width="700px" Height="80px" TextMode="MultiLine"
    onkeyup="cnt(this)" MaxLength="100"></asp:TextBox>
<p style="text-align: right;">
    <asp:Label ID="lblcharcnt" runat="server" Text="100"></asp:Label>
</p>
<asp:Label ID="lblcount" runat="server" Text='<%#Eval("row") %>' Visible="false"></asp:Label>

6 个答案:

答案 0 :(得分:1)

如果你在文本框中输入超过100个字符,这将显示一个弹出窗口 ASP.Net

<asp:TextBox ID="txtQ1F0" runat="server" TextMode="MultiLine" Rows="2" Columns="35" onKeyUp="javascript:Count(this);" onChange="javascript:Count(this);" />

javascript

 function Count(text) {
        var maxlength = 100; //set your value here 
        var object = document.getElementById(text.id)  
        if (object.value.length > maxlength) {
            object.focus(); //set focus to prevent jumping
            var count1=object.value.length;
            alert('You have exceeded the comment length of 100 characters , total characters entered are : '+count1);
            object.value = text.value.substring(0, maxlength); //truncate the value
            object.scrollTop = object.scrollHeight; //scroll to the end to prevent jumping
            return false;
        }
        return true;
    }

答案 1 :(得分:1)

这是我用于此目的的代码。请注意<%= 1000 - TextBox1.Text.Length %>的使用。这将确保在PostBack或设置初始值后显示正确的剩余字符。

<asp:TextBox ID="TextBox1" onKeyUp="setMaxLength(this)" isMaxLength="1000" runat="server" TextMode="MultiLine"></asp:TextBox>

<span id="<%=TextBox1.ClientID %>_remain"><%= 1000 - TextBox1.Text.Length %></span>

<script type="text/javascript">
    function setMaxLength(control) {
        //get the isMaxLength attribute
        var mLength = control.getAttribute ? parseInt(control.getAttribute("isMaxLength")) : ""

        //was the attribute found and the length is more than the max then trim it
        if (control.getAttribute && control.value.length > mLength) {
            control.value = control.value.substring(0, mLength);
            alert('Length exceeded');
        }

        //display the remaining characters
        var modid = control.getAttribute("id") + "_remain";
        if (document.getElementById(modid) != null) {
            document.getElementById(modid).innerHTML = mLength - control.value.length;
        }
    }
</script>

答案 2 :(得分:1)

javascript中有变化吗?尝试一下,如果需要进一步的帮助,请告诉我。

function cnt(text) {
        var a = text.value;
        var b = "character left.";
        if (a.length > 100) {
            alert('length grater than 100.');
        } else {
            text.parentNode.getElementsByTagName('span')[0].innerHTML = 100 - a.length + " " + b;
        }
    }

答案 3 :(得分:1)

我已经重写了下面的JavaScript函数。现在,用户无法输入发布最大长度。用户将被限制为仅输入100个字符

     function multilineTextBoxKeyDown(textBox, e, maxLength) {
        var selectedText = textBox.value;
        var b = "character left.";
        if (!checkSpecialKeys(e)) {
            var length = parseInt(maxLength);
            if (textBox.value.length > length) {
                textBox.value = textBox.value.substring(0, maxLength);
                textBox.parentNode.getElementsByTagName('span')[0].innerHTML = maxLength - textBox.value.length + " " + b;
                alert('Maximum no of characters reached'); //go on with your own comment
            }
            else {
                textBox.parentNode.getElementsByTagName('span')[0].innerHTML = maxLength - textBox.value.length + " " + b;
            }
        }
        else {
            //Below code shows how many characters left on deleting the text
            textBox.parentNode.getElementsByTagName('span')[0].innerHTML = maxLength - textBox.value.length + " " + b;
        }
    }

    function checkSpecialKeys(e) {
        if (e.keyCode != 8 && e.keyCode != 9 && e.keyCode != 33 && e.keyCode != 34 && e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40 && e.keyCode != 46) {
            return false;
        } else {
            return true;
        }
    }

另外,请调用下面的JavaScript

<asp:TextBox ID="txtarea" runat="server" Width="700px" Height="80px" TextMode="MultiLine"  onkeyup="multilineTextBoxKeyDown(this,event,'100')" MaxLength="100"></asp:TextBox>
<p style="text-align: right;">
<asp:Label ID="lblcharcnt" runat="server" Text="100"></asp:Label>

现在测试代码。此外,删除文本框中的文本

时将显示剩余字符数

答案 4 :(得分:0)

希望有所帮助

HTML:

  <asp:TextBox ID="yourid" runat="server" maxlength="100" ></asp:TextBox>

脚本:

  $("#yourid").keyup(function ()
    {
        var a=$("#yourid").val();


        if(a.length>99)
        {
            alert("error");
        }


    });

答案 5 :(得分:0)

它会帮助你,试试这个,

Javascrtipt

    $("#textboxId").keyup(function ()
    {
            var Textboxvalue = $("#textboxId").val();


            if (Textboxvalue .length > 100)
            {
                alert("Should be 100 characters only");
            }


        });

HTML

  <asp:TextBox  ID="textboxId" runat="server" MaxLength="100"></asp:TextBox>