I am making a form using javascript
, in the form I use an input of type date
and I would like to learn a way to be able to limit the year of the input to 4 digits, because if I add more than 4 digits the year that I get from said input is altered, for so to speak
For example, if the date is01/04/2019
When doing a console.log()
, it returns me: 2019-04-01
.
But at the time of placing 01/04/201952
, it returns me: 201952-04-01
and the date is not the one I want, if it is not an error.
Is there a way to limit the digits of the date?
- Code:
$(".formulario").change(function (){
var fecha = $("#fecha").val();
console.log('\n');
console.log(fecha);
console.log('\n');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<label for="fecha">Fecha</label>
<input type="date" name="fecha" id="fecha" class="formulario">
</form>
It occurs to me that you do it in the following way
min
andmax
you establish the date ranges that will appear available in the calendar so that the user can choosevalue
chosen date is a string , then we cansplit
convert it to a vector and read the value of index 0, which would be the year.change
Code
you can try using the min and max attributes to limit to 4 digits, for example: