Create a character counter in an input with JavaScript

Whether it's for choosing a nickname or for any other important data, you may need to limit the number of characters your users can enter in a form field. In this article, we'll look at how to do this simply and effectively.
•
Updated on August 14, 2026
•
2 min read
Believemy logo

Whether it's for choosing a username or for any other important data, you might need to limit the number of characters that your users can enter in a form field. Let's explore together in this article how to achieve this in a simple and effective way.

Image

 

Step 1

The HTML code.

HTML
<label>
  <input type="text" placeholder="Title" maxlength=20>
  <span></span>
</label>

We only have a label enclosing an input tag and a span.

 

Step 2

It's now time to style the label and the input.

CSS
label {
  position: relative;
}

input {
  font-size: 1.4rem;
  padding: .8rem 2rem .8rem 0;
  background: transparent;
  color: #F5F8FF;
  border-bottom: 1.5px solid rgba(245, 248, 255, .4);
  transition: all .4s;
  outline: none;
}

We now proceed to style the label and the input.

CSS
label span {
  position: absolute;
  right: 0;
  top: 50%;

  transform: translateY(-50%);
  width: 1.6rem;
  height: 1.6rem;

  display: flex;
  align-items: center;
  justify-content: center;

  font-size: .8rem;
  color: #F5F8FF;
  background: rgba(245, 248, 255, .2);
  border-radius: 5px;
}

In this step, we position our counter using absolute and perfectly center it thanks to the transform property, which pushes the counter upwards by taking half of its size (50%).

 

Step 3

Let's style the input on focus and the placeholder.

CSS
input:focus {
  border-color: #FF4754;
}

input::placeholder {
  color: rgba(245, 248, 255, .4);
}

To ensure that we do not display a negative number of characters, we finish the function with a simple return.

Step 4

Let's style the counter (span).

CSS
label span {
  position: absolute;
  right: 0;
  top: 50%;

  transform: translateY(-50%);
  width: 1.6rem;
  height: 1.6rem;

  display: flex;
  align-items: center;
  justify-content: center;

  font-size: .8rem;
  color: #F5F8FF;
  background: rgba(245, 248, 255, .2);
  border-radius: 5px;
}

In this step, we use Flexbox (display: flex) to horizontally and vertically center the number of characters to display.

 

Step 5

Finally, let's retrieve the necessary elements for our counter with JavaScript.

JAVASCRIPT
const input = document.querySelector('input');
const counter = document.querySelector('label span');
const maxLength = input.getAttribute('maxlength');

We retrieve the value of the maxlength attribute to display it in the span tag.

 

Step 6

All that's left is to detect a change in the number of characters.

JAVASCRIPT
input.addEventListener('input', event => {
  const valueLength = event.target.value.length;
  const leftCharLength = maxLength - valueLength;

  if (leftCharLength < 0) return;
  counter.innerText = leftCharLength;
});

To ensure that we do not display a negative number of characters, we finish the function with a simple return.

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Display a number progressively on a counter with JavaScript

When we have numbers to display, we often present them blandly and without animation. And yet: creating an animation that progressively displays the final number is very easy to do and will enable you to liven up your numbers! Let's see how in this article.

December 11, 2024

Creating a random colour generator with JavaScript

Want to practise your JavaScript skills? Try building a random colour generator! In this article, we'll look at how to do it.

December 9, 2024

The out-of-range selector (for styling when a value is not correct)

Have you always wanted to dynamically check the values entered by your users in a form field without using JavaScript? Here's how to change the style of a field when a number entered by the user is invalid!

December 9, 2024

Create a live counter of Instagram followers

Here's how to create a live Instagram follower counter that updates automatically every second! Very useful for your websites! We're using the very latest JavaScript feature: fetch!

December 9, 2024

Detecting the shift key on a form with JavaScript

Whether it's for your registration or login forms, or even on your password change pages, warning your users when they use the shift key on their keyboard will considerably improve their experience on your site. In this article, we'll show you how to do this very easily!

December 9, 2024

Creating a form with dynamic labels

The more modern a form, the more engaging it is. In this article, we'll look at how to create a form with dynamic labels that will increase the engagement of your forms and make your users want to fill them in.

December 9, 2024

Creating a clock using JavaScript

Imagine a clock that shows the time to your users, on a travel site for example, with the time zone of the destination country? Let's create this magnificent clock together, using JavaScript!

December 9, 2024

Animate emojis like on Discord with JavaScript

Would you like to create a button whose emoji changes every time you move your mouse over it? Like on Discord? Then this article is for you!

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.