How to set maxlength for multiline TextBox?

前端 未结 7 734
慢半拍i
慢半拍i 2020-12-16 01:51

When using a MultiLine TextBox (which generates a TextArea) setting the MaxLength property has no effect. What is the best workaround? I\'d like to get basic, intended funct

相关标签:
7条回答
  • 2020-12-16 02:29

    Here's a cross browser solution :

    <asp:TextBox TextMode="MultiLine" runat="server" ID="txtPurpose" Columns="50" Rows="2" onkeypress="return isokmaxlength(event,this,255);" ClientIDMode="static"></asp:TextBox>
    

    Javascript :

    function isokmaxlength(e,val,maxlengt) {
        var charCode = (typeof e.which == "number") ? e.which : e.keyCode
    
        if (!(charCode == 44 || charCode == 46 || charCode == 0 || charCode == 8 || (val.value.length < maxlengt))) {
            return false;
        }
    }
    

    You have to think about the Copy and Paste. This is a little bit tricky, I simply disable it with Jquery. But you can create your own function to do more complex verification. But in my case, copy and paste is not allowed.

    Jquery to disable copy and paste :

    jQuery(function ($) {
    
        $("#txtPurpose").bind({
            paste: function (e) {
                e.preventDefault();
            }
        });
    
    }); 
    
    0 讨论(0)
提交回复
热议问题