---
title: "How to prevent a user from adding same character over and over in HTML input box using JavaScript?"  
description: "How to prevent a user from adding same character over and over in HTML input box using JavaScript?"  
author: "Revati S Misra"  
published: 2023-01-24  
updated: 2023-04-06  
canonical: https://www.mindstick.com/forum/157343/how-to-prevent-a-user-from-adding-same-character-over-and-over-in-html-input-box-using-javascript  
category: "html"  
tags: ["jquery", "javascript", "html"]  
reading_time: 1 minute  

---

# How to prevent a user from adding same character over and over in HTML input box using JavaScript?

How to prevent a [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) from adding same [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) over and over in [HTML input](https://www.mindstick.com/forum/34517/how-to-find-html-input-box-value-using-jquery) box using [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Aryan Kumar

To prevent a user from adding the same character repeatedly in an [HTML](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) box, you can use JavaScript to check for the last entered character and block the user from entering the same character again.

```javascript
const input = document.getElementById('myInput');
let lastChar = '';
input.addEventListener('input', (event) => {
  const value = event.target.value;
  const currentChar = value.charAt(value.length - 1);
  if (currentChar === lastChar) {
    event.target.value = value.slice(0, value.length - 1);
  }
  lastChar = currentChar;
});
```


---

Original Source: https://www.mindstick.com/forum/157343/how-to-prevent-a-user-from-adding-same-character-over-and-over-in-html-input-box-using-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
