Created 2 events onekeyup and onchange (for checking just the length of not more than ten digits). The function does not work correctly. How to set the conditions so that you can enter no more than ten digits? Thank.

 <input type="text" maxlength="10" name="answer" id="t" onkeyup="isAllowedSymbol(this);" onchange="checkLength();" placeholder="Enter data" > function checkLength() { var element = document.getElementById('t'); if(document.createEvent) { var e = document.createEvent('HTMLEvents'); e.initEvent('change', false, true); element.dispatchEvent(e); alert("ivalid length - 10 characters only!"); return element; } } 

limit the input of elements, roughly speaking, so that the elements would be no more than 10

maxlength for keyboard input, not via buttons

  • one
    You probably did not formulate the question correctly. should your function limit the input or output a message that input is limited? for you limit the number of characters through the input attribute, and not through js - lexxl
  • elements or numbers? - Alex

1 answer 1

http://htmlbook.ru/html/input/maxlength

the function is not needed at all in this case, since character input can be limited to the maxlength attribute that the input tag has. For example:

 <input maxlength = "10"/>