Input Field | SoloLearn: Learn to code for FREE!

+6

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

+2

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. https://code.sololearn.com/Whn0B3Ak5pJM/#html

+7

ODLNT what is that 'isNanN' in if statement

+6

ODLNT I had tried this, but it is not working. https://code.sololearn.com/W8u3W3bJPZ2C/?ref=app

+6

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.😄

+6

Thanks ODLNT 😊

+5

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

+1

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

+1

<input type="text" name="usrname" maxlength="4"> https://www.w3schools.com/tags/att_input_maxlength.asp

+1

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. https://www.w3schools.com/jsref/jsref_isnan.asp

+1

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. https://code.sololearn.com/WIEKd1anK5rd/#html Good luck, good coding! 🤞💻