HTML - ` part without any JavaScript. – Iresha Rubasinghe Jan 26 '16 at 02:57

4 Answers4

3

Try simple HTML solution with maxlength

maxlength Declares an upper bound on the number of characters the user can input. Normally the UI ignores attempts by the user to type in additional characters beyond this limit.

<textarea maxlength="4000"></textarea>

There is an alternate with jQuery plugin jQuery Max Length

Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58
  • @Divyesh It is compatible with all major browsers. check here: http://caniuse.com/#search=maxlength – Vicky Gonsalves Jan 25 '16 at 08:37
  • i don't allow user to enter or add content an textarea if maxlength exceed – Divyesh Jan 25 '16 at 08:39
  • Did u comment all javascript code and try? This attribute doesn't require any javascript. – Vicky Gonsalves Jan 25 '16 at 08:42
  • @ Vicky in chrome : if i past the text that contain text with new lines and now if i remove last character it will not allow me to enter again at the last. – Divyesh Jan 25 '16 at 08:47
  • @ Vicky Gonsalves : Please try this fiddle in chrome browser.....you will understand my problem https://jsfiddle.net/j2pz0tjg/2/ – Divyesh Jan 25 '16 at 09:38
  • Its because you have 3 linebreaks in the data.. remove 3 or more characters and try entering again.. – Vicky Gonsalves Jan 25 '16 at 09:42
  • I show this link before a day,.... But how can i say to user that please do not break the line while past the text ...?? – Divyesh Jan 25 '16 at 10:00
  • So don't you count whitespaces as well? – Vicky Gonsalves Jan 25 '16 at 10:09
  • I have made my own logic same as your above link....but If textarea length exceeds its limit, and we give the input in the middle of the text than it start the truncate character from the end. But i don't want this. – Divyesh Jan 25 '16 at 10:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101576/discussion-between-divyesh-and-vicky-gonsalves). – Divyesh Jan 25 '16 at 10:47
2

simply use this:-

 <textarea maxlength='4000'  rows="20" cols="40" id="Comments"></textarea> 

it automatically stops when user types 4000 characters. That means though user type more than 4000 characters, they don't get insert into the text area.

If you further wants to prompt an warning message to user when the limit exceeds, just add the below simple JavaScript piece of code.

$(function() {  
    $("textarea[maxlength]").bind('input propertychange', function() {  
        var maxLength = $(this).attr('maxlength');  
        if ($(this).val().length >= maxLength) {  
            alert("Hello! I am an alert box!!");
            $(this).val($(this).val().substring(0, maxLength));  
        }  
    })  
});
Iresha Rubasinghe
  • 913
  • 1
  • 10
  • 27
2

Use this:

$('#Comments').keydown(function (key_event) {
                    var text_value = $('#Comments').val();
                    var len=text_value.length;
                    if (len > maxlength) {      
                        key_event.preventDefault();
                    }
                    cursorPosition = $('#Comments').prop("selectionStart");
                    countChar(key_event);
            }); 
hvnis
  • 91
  • 4
1

On input check if the value in the textbox is >= 4000, then disable the textbox element.

Check this link for more info about textarea and the disabled attribute. http://www.w3schools.com/tags/att_textarea_disabled.asp

Johan Brännmar
  • 886
  • 7
  • 21