This question has already been answered:

It is necessary to check if the length does not exceed 10 elements.
I use getElementById , but it doesn't work, what's the problem?
Plus, it rechecks only what you typed on the keyboard, and how to make an additional input check at the touch of a button?

 function checkLength() { var input = document.getElementById("t"); { if (input.value.length == 10) { input = input.replace(input, ''); alert("Only 10 numers"); } } } 
 <input type="text" maxlength="10" name="answer" id="t" onkeyup="isAllowedSymbol(this);checkLength();" placeholder="Enter data"> 

Reported as a duplicate by Grundy , Athari , VenZell , Pavel Mayorov , fori1ton participants on Apr 25 '16 at 14:58 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • The maxlength property no longer gives input of more than 10 characters, what else is needed? - user207618
  • maxlength for the keyboard, and how then to check input events through the button? - Cergiy Krepkiy
  • Um ... What is the "button"? - user207618
  • I have a calculator, the numbers that I enter through the buttons do not check, and the numbers that I enter with the keyboard check how to do that when entering from the buttons the input field is checked for maxlength? - Cergiy Krepkiy
  • Maybe in the formulation of the problem it was necessary to hint that you are using exotic input, do you guess? Nobody will give a normal answer, since it is not known what the calculator is, what it still does and why something is not working there. Add a description of this calculator to the question. - user207618

1 answer 1

Here is the simplest test:

 function checkLength(e){ if(e.value.length > 10){ // Если больше 10 e.value = ''; // Очищаем поле (вроде же так задумывалось?) alert("Only 10 numers"); // Выводим сообщение } } function isAllowedSymbol(e){ return true; // Заглушка } 
 <input type="text" name="answer" id="t" onkeyup="isAllowedSymbol(this);checkLength(this);" placeholder="Enter data">