skip to Main Content

The alert line activates it and the input field locks, preventing the player from entering text.

I have added the restriction of a maximum of five letters, but when I add a minimum using ||, this restriction blocks me and I cannot enter the text.I need it to prevent the player from entering words less than 5 letters.

I need help, I’m a beginner.

enter image description here

2

Answers


  1. That’s expected, because you are utilising input event. If I understand correctly, you are trying to use (input.value.length<5||input.value.length>5) for this. In this case, the input event will be fired with every keypress and the if statement will be executed everytime till the length becomes 5. That’s because input.value.length<5 will be true, till the length is not equal to 5.

    What you can do is, use change event instead of input. The change event will be fired when the input is changed and loses the focus or the user presses the enter key. Then the if clause can check for the conditions to determine whether the input is alright or not.

    Login or Signup to reply.
  2. There you go:

    var x = document.getElementById("myText").maxLength; 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search