JavaScript Character Count Script

Learn how to create a character counter using JavaScript. A step-by-step guide with examples, common mistakes, and beginner-friendly tips.

Character Counter in JavaScript

Have you ever filled a form online where you can only type a certain number of characters in a message box or comment box? You may have noticed a small text that says something like "125 characters left" while typing. This feature is called a character counter. In this article, we will learn how to make it using simple JavaScript.

Why Do We Need a Character Counter?

Character counters are useful when you want to:

  • Limit the size of input in a text area or input box.
  • Show users how much they have typed and how much space is left.
  • Improve the user experience in online forms.
  • Avoid storing too much text in databases.

Where Is Character Counter Used?

You can find character counters in many places such as:

  • SMS or online message services (like free SMS websites)
  • Social media post boxes (like Twitter or Instagram captions)
  • Feedback or comment forms
  • Signup forms or job applications
  • Support ticket forms where input must be short

How Does Character Counter Work?

The idea is simple. Every time you type something, JavaScript counts the number of characters. If the number goes beyond a set limit, it stops or cuts off extra characters. It also shows how many characters are left.

Example JavaScript Code

Below is a simple JavaScript function to create a character counter. It works with a form that has a textarea and a small box showing how many characters are left.

<script type="text/javascript">
// You can change this number to increase or decrease the limit
var count = 125;

function limiter() {
    var textvalue = document.demoform.message.value;
    var len = textvalue.length;
    if (len > count) {
        var tex = textvalue.substring(0, count);
        document.demoform.message.value = tex;
        return false;
    }
    document.demoform.limit.value = count - len;
}
</script>

Example HTML Code

Now let's add the HTML part to make it work.

<form name="demoform" method="post">
    <table>
        <tr>
            <td><strong>Characters Counter Demo</strong></td>
        </tr>
        <tr>
            <td colspan="2">
                <textarea name="message" rows="3" cols="40" onkeyup="limiter()"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <label>Characters left:</label>
            </td>
            <td>
                <input type="text" name="limit" size="4" readonly value="125">
            </td>
        </tr>
    </table>
</form>

Steps to Use the Character Counter

  1. Copy the JavaScript code into the head part or before the closing </body> tag of your HTML file.
  2. Paste the HTML code where you want the form to appear.
  3. Change the limit by editing the value of the variable count in JavaScript.
  4. Test it in your browser.

Things to Remember

  • Always validate the input on the server side also.
  • This counter only works when the user types using a keyboard. It may not catch pasted text unless handled properly.
  • You can also use oninput instead of onkeyup for better control.
  • If you are using jQuery or a framework, there are easier ways to do this.

Common Mistakes Beginners Make

  • Forgetting to use quotes around HTML attribute values like onkeyup="limiter()".
  • Setting a wrong limit number (like 1250 instead of 125).
  • Not checking empty input or null values.
  • Trying to apply this logic on non-text fields like checkboxes.

Use Case: Limiting Feedback Text

Suppose you are making a website that takes user feedback. You don’t want users to type too much, so you want to limit it to 300 characters. Just set var count = 300; in your JavaScript. The counter will now allow only 300 characters.

Use Case: SMS Message Counter

Most SMS services allow only 160 characters per message. If you are building a free SMS tool, you can use this JavaScript character counter to stop the user from typing more than 160 characters. This helps in controlling how much data is stored or sent.

Can I Style the Counter?

Yes, you can use CSS to style the counter box. For example, you can change its color when characters are less or almost used up. You can also make the textarea border red if the user types more than allowed characters (with additional JavaScript).

Final Thoughts

Character counters are small but very useful features. They help users know how much they can type. They also help developers control and clean the data that comes from users.

This is a great mini project if you are learning JavaScript. Try making it better by adding warning messages or using a progress bar to show usage.

What You Learned

  • Why character counters are useful
  • How to create a character counter with simple JavaScript
  • How to show characters left while typing
  • Best practices and common mistakes

Hope this article helped you. If yes, feel free to try it out in your projects or share it with others who are learning JavaScript!

0 Comments
Leave a Comment