Copy Code code as follows:
<!doctype html>
<meta charset= "Utf-8" >
<title> Date Input </title>
<script>
Window.onload = function () {
var alaydate = document.getelementsbyclassname ("date");
for (var i = 0;i < Alaydate.length;i + +)
{
Alaydate[i].onchange = function () {
var dateValue = This.value;
DateValue = Datevalue.replace (/\. /g, "-");
DateValue = Datevalue.replace (/\./g, "-");
if (datevalue.length = = 8) {
var temp = datevalue.substring (0,4) + "-" + datevalue.substring (4,6) + "-" + datevalue.substring (6,8);
DateValue = temp;
Console.log (DateValue);
}
if (CHECKDT (DateValue)) {
This.value = DateValue;
}
Else
{
Alert ("Date input error");
}
}
}
}
function Checkdt (str)
{
var r = Str.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/);
if (r==null)
{
return false;
}
Else
{
var d= new Date (r[1], r[3]-1, r[4]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]);
}
}
</script>
<body>
<input placeholder= "Please enter date" class= "dates" >
</body>
Put the input of YYYY.MM.DD, YYYY. Mm. DD, YYYYMMDD to Yyyy-mm-dd
Checkdt This function was found in the Niang.
Very simple and practical functions, the small partners can be used directly.