简体   繁体   中英

Input field with type date or type text

I have a Laravel app with a date field.

When I use "type=text"

<input type="date" class="form-control" id="startdate" placeholder="Start date" name="startdate" />

I get the value that is stored in my database.

When I use "type=date"

<input type="date" class="form-control" id="startdate" placeholder="Start date" name="startdate" />

I get dd/mm/yyyy and a dropdown (datepicker) to select a number for each of these. I would have expected that the dd, mm and yyyy would be pre-filled with the date from my database.

While input=text is working, I don't like that as the uses can then type in any text in that field.

What is the best way to do this? Keep the input=date and fix that or put it to input=text and do server side validation?

Try this

function getDefaultDate(curDate){
var dt = new Date(curDate);
var date = dt.getDate();
var month = dt.getMonth();
var year = dt.getFullYear();
if (month.toString().length == 1) {
    month = "0" + month
}
if (date.toString().length == 1) {
    date = "0" + date
}
return year.toString() + "-" + month.toString() + "-" + date.toString();
}

var fdate = getDefaultDate(curDate); // pass db date

document.getElementById("startdate").value = fdate;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM