-2

Even if the format is dd/mm/yyyy I see that I can insert more than only 4 characters in the year. I don't know how to stop this behavior.

<html>
<head>
<title>input test data insert</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
   <input type="date" data-date-format="dd/mm/yyyy">
</body>
</html>
Erfan Bahramali
  • 392
  • 3
  • 13
  • Is this the answer to your question? https://stackoverflow.com/questions/24603919/html5-date-input-6-digit-year – Erfan Bahramali May 05 '21 at 11:46
  • If you prevent the application from accepting a year of more than 4 digits, it will suffer from [the Y10K problem](https://en.wikipedia.org/wiki/Year_2000_problem). – Armen Michaeli May 05 '21 at 12:03

2 Answers2

0

Add a max attrubute and set it to max="9999-12-31"

<html>
<head>
<title>input test data insert</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
   <input type="date" data-date-format="dd/mm/yyyy" max="9999-12-31">
</body>
</html>
Dumisani
  • 2,988
  • 1
  • 29
  • 40
0

You can specify a max date allowed in the input (keep in mind that 10000AD is a real year, even if it is a long way off), but it will only be checked when the form is submitted.

<form>
  <input type="date" data-date-format="dd/mm/yyyy" max="9999-12-31">
  <button>Submit</button>
</form>

You can add JS to check the value when it changes, but you'll need to provide your own UI for showing the error.

document.querySelector('input').addEventListener('change', event => {
  if (!event.target.checkValidity()) alert("Error")
});
<form>
  <input type="date" data-date-format="dd/mm/yyyy" max="9999-12-31">
  <button>Submit</button>
</form>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335