1 like 0 dislike
545 views

Need to limit the number of characters that are allowed to be entered into a textarea?

 

in Education & Reference by | 545 views

1 Answer

0 like 0 dislike
Best answer

The Javascript

 
<script type="text/javascript" src="http://www.google.com/jsapi"></script>  
      
    <script type="text/javascript" language="javascript">  
    google.load("jquery", "1.4.2");  
      
    var characterLimit = 150;  
      
    google.setOnLoadCallback(function(){  
          
        $('#remainingCharacters').html(characterLimit);  
          
        $('#myTextarea').bind('keyup', function(){  
            var charactersUsed = $(this).val().length;  
              
            if(charactersUsed > characterLimit){  
                charactersUsed = characterLimit;  
                $(this).val($(this).val().substr(0, characterLimit));  
                $(this).scrollTop($(this)[0].scrollHeight);  
            }  
              
            var charactersRemaining = characterLimit - charactersUsed;  
              
            $('#remainingCharacters').html(charactersRemaining);  
        });  
    });  
 </script> 
 

The HTML

 <textarea id="myTextarea"></textarea>  
<p><span id="remainingCharacters"></span> characters remaining.</p> 

 

by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
2 like 0 dislike
1 answer
1 like 0 dislike
1 answer
1 like 0 dislike
3 answers
0 like 0 dislike
1 answer
asked Jan 22, 2016 in Education & Reference by Krish (820 points) | 177 views
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,225 questions
1,370 answers
559 comments
2,099 users