54

I have a standard <input /> control on my form, decorated with type="date"

When rendered, it displays the correct watermark of yyyy-mm-dd, and you can select a date correctly.
However, when you try type in a value, the year extends to 6 digits, instead of four. I have added screenshots to help demonstrate the issue I'm having.

Is anyone else getting this? I'm using Chrome ( Version 35.0.1916.153 m ) as my default browser.
I'd like a way to force a 4year input that doesn't involve extra JS.

Date Control

Control Source

TheGeekZn
  • 3,696
  • 10
  • 55
  • 91

5 Answers5

30

the max attribute is working, as far as i know...

the format of max is trickey, if you put the correct format everything will work fine....

see here for example:

http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_input_max_min_date

Yaniv Hadad
  • 301
  • 3
  • 2
23

Use max attribute to fix this problem like:-

<input type="date" max="1979-12-31">
Javed
  • 1,613
  • 17
  • 16
16

A bit old, but for posterity...

Using the max attribute eliminates this problem.

<input type="date" max="2999-12-31">

Pen: https://codepen.io/iHearRobots/pen/OQVzLZ

Cam Beaudoin
  • 185
  • 1
  • 2
  • 7
    I don't agree that this "eliminates" the problem. As of Chrome 70, for example, I can type a 6 digit year. IMO, it should stop me from typing past the number of digits in the max year. It might get "validated" as incorrect, but validations are still not standard. – Andrew Grothe Nov 02 '18 at 14:20
  • 1
    This only seems to work if the users use the little toggle up and down buttons (in Chrome at least). It doesn't prevent typing data outside of the range though. – Zachary Weber Oct 22 '19 at 20:26
  • This won't work in Safari though https://caniuse.com/input-datetime right now – zyrup Dec 12 '21 at 16:41
  • On firefox you can still input a longer year. Max only works for 4 digit years – 0x777C Oct 18 '22 at 10:21
9

It will let you type a larger year. It wasn't designed with a cap, so to allow years beyond 9999. Not sure why. (note, even with the max attribute, it doesn't appear to restrict it. [edit] .. it may not allow the user to actually submit after entering that larger number.)

You will need to use a JavaScript solution if you want to fully restrict it.

See this previous thread

Community
  • 1
  • 1
John C
  • 666
  • 4
  • 8
0
const maxLengthCheck (event) ->{ 
    let date = event.target.value
    if(date){
        let dateArr date.split('-') 
        if(dateArr[0] && dateArr[0].length>4){ 
             dateArrl[0]=dateArr[0].substr(e, dateArr[0].length-1) 
             date = dateArr.join('-')
             console.log("updated date : ", date) 
             document.getElementById('date-input').value = date
        }
    }
}

//HTML changes
<input id="date-input" type="date" onInput={maxLengthCheck}/>
Tushar Kale
  • 159
  • 1
  • 12