-1

I want to limit the number of characters from 3 to 10 in an input form. How can I manage it using Javascript. I need to do it with Javascript so I can change the input field border color from black to red when user's input doesn't meet the requirements. Here is my HTML code. I have tried some variants but got into a mess. Please provide me with the simple solution. Thanks beforehand.

<form action="/index" method="POST">
  <input type="text" id="firstname" name="firstname" placeholder="First name">
  <br>
  <input type="text" id="lastname" name="lastname" placeholder="Last name">
</form>
Albertina
  • 1
  • 2
  • 2
    Does this answer your question? [Limit number of characters allowed in form input text field](https://stackoverflow.com/questions/8545376/limit-number-of-characters-allowed-in-form-input-text-field) – Ben Fortune Sep 25 '20 at 11:47

2 Answers2

0

You could try something like this;

<input type="text" id="lastname" name="lastname" placeholder="Last name" minlength="3" maxlength = "10">
Aleksandar Zoric
  • 1,343
  • 3
  • 18
  • 45
-1

Use maxlength and minlength:

<form action="/index" method="POST">
  <input type="text" id="firstname" name="firstname" placeholder="First name" minlength="3" maxlength="10">
</form>
kingkong.js
  • 659
  • 4
  • 14