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.
2
Answers
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, theinput
event will be fired with every keypress and the if statement will be executed everytime till the length becomes 5. That’s becauseinput.value.length<5
will be true, till the length is not equal to 5.What you can do is, use
change
event instead ofinput
. Thechange
event will be fired when the input is changed and loses the focus or the user presses the enter key. Then theif
clause can check for the conditions to determine whether the input is alright or not.There you go: