Input Field | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

9th Jul 2019, 7:50 AM
Gurpreet Jhamat
Gurpreet Jhamat - avatar
10 Answers
+ 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
9th Jul 2019, 9:32 AM
ODLNT
ODLNT - avatar
+ 7
ODLNT what is that 'isNanN' in if statement
9th Jul 2019, 9:46 AM
Gurpreet Jhamat
Gurpreet Jhamat - avatar
+ 6
ODLNT I had tried this, but it is not working. https://code.sololearn.com/W8u3W3bJPZ2C/?ref=app
9th Jul 2019, 9:00 AM
Gurpreet Jhamat
Gurpreet Jhamat - avatar
+ 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.😄
9th Jul 2019, 10:03 AM
Gurpreet Jhamat
Gurpreet Jhamat - avatar
+ 6
Thanks ODLNT 😊
9th Jul 2019, 10:28 AM
Gurpreet Jhamat
Gurpreet Jhamat - avatar
+ 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
9th Jul 2019, 9:30 AM
Gurpreet Jhamat
Gurpreet Jhamat - avatar
+ 1
Javascript! Get the input, check its length, block if length exceed 4.
9th Jul 2019, 8:04 AM
Drax
Drax - avatar
+ 1
<input type="text" name="usrname" maxlength="4"> https://www.w3schools.com/tags/att_input_maxlength.asp
9th Jul 2019, 8:49 AM
ODLNT
ODLNT - avatar
+ 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
9th Jul 2019, 9:54 AM
ODLNT
ODLNT - avatar
+ 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! 🤞💻
9th Jul 2019, 10:25 AM
ODLNT
ODLNT - avatar