Home > Web Front-end > JS Tutorial > body text

JavaScript function to check date format [Comprehensive]_Time and date

WBOY
Release: 2016-05-16 18:59:47
Original
1025 people have browsed it

//Text box name for date = "indate"
function check1(Form)
{
for (i = 0; i {
if (Form.elements[i].value == "")
{
alert("Please fill in completely!")
Form.elements[i].focus();
return;
}

if (Form.elements[i].name == "indate")
{
var date = Form.elements[i].value;
len = date .length;
var datearray = date.split("-")
//alert(datearray[1]);
var year = parseInt(datearray[0]);
var month = parseInt(datearray[1]);
var day = parseInt(datearray[2]);

if (len == "")
{
alert("Time cannot be empty ");
return;
}
else if (len > 10)
{
alert("Date format is incorrect");
return;
}
else if (isNaN(year))
{
alert("Date format is incorrect");
return;
}
else if ((year > 9999) || (year {
alert("Date format is incorrect");
return;
}
else if (isNaN(month))
{
alert( "The date format is incorrect");
return;
}
else if ((month > 12) || (month {
alert("The date format is incorrect ");
return;
}
else if (isNaN(day))
{
alert("Date format is incorrect");
return;
}
else
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if ((dat 31))
{
alert("Date format is incorrect");
return;
}
break;
case 4:
case 6:
case 9:
case 11:
if ((day 30))
{
alert( "Date format is incorrect");
return;
}
break;
default:
if ((year % 100 == 0) && (year % 4 == 0))
{
if ((day 29))
{
alert("Date format is incorrect");
return;
}
}
else
{
if ((day 28))
{
alert("Date format is incorrect");
return;
}
}
}
}
}
}
Form.submit();
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template