Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How i give maxlength to Editable Div as textarea.

i am using this code and it not working fine

ASP.NET
<div class="textarea-Simple" style=" line-height:1; margin: 10px 0 0 0;resize:vertical;height:30px; border: 1px solid #CDCDCD;" id="textareaid"  runat="server" onkeyup="DetectChange();" onpaste="DetectChange();" contenteditable="true"></div>


Can any body have an idea?
Posted
Updated 3-Dec-20 10:08am
v3
Comments
Member 11732163 14-Oct-15 6:08am    
There is no maxlength for div. you can control it in the keypress event of div
[no name] 14-Oct-15 6:22am    
if any code paste it.

1 solution

Created your div as editable..

ASP.NET
<div class="textarea-Simple" style=" line-height:1; margin: 10px 0 0 0;resize:vertical;border: 1px solid #CDCDCD;" id="txtAreaNew" runat="server" contenteditable="true" data-maxlength="300" data-minlength="50"></div>

<span id="error">Min 50 character or max 300 character required</span>


now you have to check text count on every key up by below jquery code..

C#
$(document).ready(function() {
        $('#txtAreaNew').keyup(function(){
            var limit = parseInt($(this).attr('data-maxlength'));
            var minlimit = parseInt($(this).attr('data-minlength'));
            var text = $(this).html();
            var chars = text.length;
            if(chars >= limit){
                $('#error').show();
                var new_text = text.substr(0,limit);
                $(this).html(new_text);
            }else if(chars < minlimit){
                $('#error').show();
            }else if(chars == 0){
                $('#error').hide();
            }else{
               $('#error').hide();
            }
        });
    });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900