我正在尝试编写验证以使用 javascript 检查日期格式。但是我仍然有错误消息日期格式无效即使我输入了正确的日期格式我不知道我的正则表达式是否正确我在网上找到了它
谢谢你的帮助
function CheckDateFormat()
{
var StartDateform= document.getElementById('tblTarget').rows[1].cells[StartDate].getElementsByTagName('input')[0].value;
var pattern;
// Regular expression used to check if date is in correct format
pattern= new RegExp("^([0-9]{2})-([0-9]{2})-([0-9]{4})$");
if(StartDateform.match(pattern))
{
var date_array;
var day;
date_array = StartDateform.value.split('-');
day = date_array[0];
// Attention! Javascript consider months in the range 0 - 11
var month;
var year;
month= date_array[1] - 1;
year= date_array[2];
// This instruction will create a date object
//source_date = new Date(year,month,day);
source_date = new Date(day,month,year);
if(year != source_date.getFullYear())
{
alert('Year is not valid!');
return false;
}
if(month != source_date.getMonth())
{
alert('Month is not valid!');
return false;
}
if(day != source_date.getDate())
{
alert('Day is not valid!');
return false;
}
}
else
{
alert('Date format is not valid!');
InvalidFlag=1;
return false;
}
return true;
}