HTML Input Attributes

placeholder, required, min, max, pattern and more.

✍️ 3 exercises + code challenge in this lesson

Input attributes add hints and built-in validation, with no JavaScript needed:

  • value: the initial value.
  • placeholder: a hint shown when the field is empty.
  • required: the field must be filled in.
  • readonly / disabled: cannot be changed (disabled fields are not submitted).
  • min, max, step: limits for numbers and dates.
  • maxlength: maximum number of characters.
  • pattern: a regular expression the value must match.
  • autofocus: focus this field when the page loads.

Key Takeaways: HTML Input Attributes

Mastering HTML Input Attributes helps you write clean and reliable HTML code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

HTML Input Attributes Code Example

HTML
<form onsubmit="alert('All fields are valid!'); return false;">
  <input type="text" name="user" placeholder="Username (required)" required autofocus><br><br>
  <input type="number" name="age" min="18" max="60" placeholder="Age 18–60"><br><br>
  <input type="text" name="code" pattern="[A-Z]{3}" title="Three capital letters" placeholder="Code e.g. ABC"><br><br>
  <input type="text" value="Read only" readonly>
  <input type="text" value="Disabled" disabled><br><br>
  <input type="submit">
</form>
Free sign-in required · Google login available

HTML Input Attributes Practice Exercises

Check what you learned in “HTML Input Attributes”.

/ 3 solved
Fill in the blank

Make the field mandatory.

<input type="text" name="name" >
✓ Correct! Not quite. Try again.
Multiple choice

Which attribute shows a hint inside an empty field?

✓ Correct! Not quite. Try again.
Code challenge

Add a number input named "age" with min 18 and max 60.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services