Muhammad Vairnd
Muhammad Vairnd

Reputation: 81

Input = time, How to allow input of only the hour. No minutes or seconds

I want to allow the user to input a time but only allow the input of hours, no minutes or seconds. I understand a text or number field would be better but need to post the input as a time.

I have tried setting the step attribute to 600, however that removes the milliseconds only.

Upvotes: 7

Views: 36953

Answers (2)

Kida
Kida

Reputation: 840

When I tried to use code suggested in accepted answer, it didn't work for me as expected, so I just want to share my experience , if someone has same problem:

In my case I had no milliseconds in my input, so it was enough to do it like this:

<input type='time' step="3600" >

Upvotes: 4

somethinghere
somethinghere

Reputation: 17330

The step attribute is the step amount you can do in milliseconds, so in this case you should use 60 * 60 * 1000 (60 minutes * 60 seconds * 1000 milliseconds = 1 hour) as the value:

<input type="time" step="3600000" />

Note that this does not work in Safari, but I did test it in Chrome. Safari simply doesnt support this type at all, so no validation happens as far as I can see. Which makes this not universally compatible.

Upvotes: 6

Related Questions