Fiad
Fiad

Reputation: 803

Force user to enter only hours in Input=Time and disable Minutes

In my application the Time will always be like HH/00/00. So users will only need to enter the hour value as the minutes will be 00. I could not find a way to change the format of the input=time to only HH.

I found some articles about assigning step value but it doesn't work.

Any idea about preventing users to select minutes and force him to only select hours?

<div class="md-form mx-5 my-5">
    </div>

  <label>Choose your time</label>
  <input type="time"  /

Time

Upvotes: 1

Views: 4465

Answers (1)

Feki Hamza
Feki Hamza

Reputation: 307

How about some JS code ? a simple one really, like the one below, you can add more conditions to make it much more strict, but here you go:

const timeInput = document.getElementById('time');

timeInput.addEventListener('input', (e) => {
  let hour = e.target.value.split(':')[0]
  e.target.value = `${hour}:00`
})
<div class="md-form mx-5 my-5">
    </div>

  <label>Choose your time</label>
  <input id="time" type="time">

Upvotes: 3

Related Questions