Javascript Bootstrap - Textarea count characters validation with example

Javascript Bootstrap - Textarea count characters validation with example

In this example, I will tell you how to implement character count and apply validation for maximum character length in textarea.

This is very useful to let you know how many characters you type into the field.

For example, for SMS field you know what limitation is required to send message by using SMS Api, there you can apply the max length validation.

You can customize the text message of remaining characters as well you can customize their position.

Bootstrap Maxlength is a very awesome jQuery plugin to validate character count on client side.

Let's have a example :

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Bootstrap Maxlength Validation with Example</title>
  5.     <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  6.     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  7.     <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8.     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-maxlength/1.7.0/bootstrap-maxlength.min.js"></script>
  9. </head>
  10. <body>
  11. <div class="row">
  12. <div class="col-md-offset-3 col-md-6">
  13.         <div class="form-group">
  14.             <textarea class="form-control" placeholder="Type here to see how it work" maxlength="100"></textarea>
  15.         </div>
  16. </div>
  17. </div>
  18.     <script type="text/javascript">
  19.         $('textarea').maxlength({
  20. alwaysShow: true,
  21. });
  22.     </script>
  23. </body>
  24. </html>

Now we will change the position of counter at top left, you can place the counter indicator on top-right, bottom-left etc.

  1.     <script type="text/javascript">
  2.         $('textarea').maxlength({
  3. alwaysShow: true,    
  4.      placement: 'top-left'
  5. });
  6.     </script>

You can also customize the message by following way:

  1. <script type="text/javascript">
  2.         $('textarea').maxlength({
  3. alwaysShow: true,
  4.      threshold: 10,
  5.      warningClass: "label label-success",
  6.      limitReachedClass: "label label-danger",
  7.      separator: ' out of ',
  8.      preText: 'You write ',
  9.      postText: ' chars.',
  10.      validate: true,
  11.      placement: 'bottom-left'
  12. });
  13.     </script>

Phone: (+91) 8800417876
Noida, 201301
Attention Required! | Cloudflare

Sorry, you have been blocked

You are unable to access ressim.net

Why have I been blocked?

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

What can I do to resolve this?

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.