HTML is there a way to limit the year to 4 digits in date input

Viewed 3258

I am developing a django app.
I have date input in my form:

<input type="date" name="date_of_birth" max="03.11.2020" max-length="8" pattern="[0-3][0-9].[01][0-9].[0-9]{4}" class="dateinput form-control" required id="id_date_of_birth">

It allows to enter the date with the year having 6 digits, but I wish it was only possible to enter 4. I also tried to write a simple script:

$(function() {
        $('#id_date_of_birth').change(function() {
            var date = $(this).val();
            console.log(date, 'change')
        });
    });

but it only starts when I change the year.
I would like the numbers to loop on 4 digits instead of 6.
Can someone give me a hint how to limit the year to only 4 digits?

2 Answers

I like the suggestion of using the attribute max. Just set the max="9999-12-31" in your input date type field and the year portion will be limited to 4 digits instead of 8. This solved my problem.

Related