Input Field | SoloLearn: Learn to code for FREE!


Input Field

How to limit the number of characters in an input field. If I have to put only four characters in an input how should I do it.

7/9/2019 7:50:18 AM

Gurpreet J

10 Answers

New Answer


Gurpreet J I did not realize that the input should be numbers only. In that case, Drax is correct you will need a script. Here is one way it could be done.


ODLNT what is that 'isNanN' in if statement


ODLNT I had tried this, but it is not working.


ODLNT thanks for answering my question. I want to ask is there any other way to do the same with a simpler syntax, if not than no problem.😄


Thanks ODLNT 😊


Drax I'm not sure how to do this with JavaScript. I try to it but not able to do so. Could you please explain how to do this


Javascript! Get the input, check its length, block if length exceed 4.


<input type="text" name="usrname" maxlength="4">


isNaN() is a function that determines whether a value is an illegal number (Not-a-Number). If the user enters, for example, "3ii34" when passed to the function isNaN() it will return a boolean true letting the user know that an illegal number was entered.


Gurpreet J Here is another version, I think this may work for you. If not I'm sure someone will come along with another alternative. Good luck, good coding! 🤞💻