JavaScript判断两种格式的输入日期的正确性的代码
2018-10-15 17:30
最简单的
复制代码 代码如下:
functionisValidDate(dateStr){
varmatchArray=dateStr.match(/^[0-9]+-[0-1][0-9]-[0-3][0-9]$/)
if(matchArray==null){
alert(Invaliddate:+dateStr);
returnfalse;
}
returntrue;
}
functionisValidDate(dateStr){
varmatchArray=dateStr.match(/^[0-9]+-[0-1][0-9]-[0-3][0-9]$/)
if(matchArray==null){
alert(Invaliddate:+dateStr);
returnfalse;
}
returntrue;
}
第二种
复制代码 代码如下:
<scriptlanguage=javascript>
String.prototype.isDate=function()
{
varr=this.match(/^(\d{1,4})(-\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)returnfalse;vard=newDate(r[1],r[3]-1,r[4]);
return(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
alert(2002-01-31.isDate());
alert(2002-01-41.isDate());
</script>
<scriptlanguage=javascript>
String.prototype.isDate=function()
{
varr=this.match(/^(\d{1,4})(-\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)returnfalse;vard=newDate(r[1],r[3]-1,r[4]);
return(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
alert(2002-01-31.isDate());
alert(2002-01-41.isDate());
</script>
第三种,比较复杂的
复制代码 代码如下:
<scriptlanguage=javascript>
String.prototype.isTime=function()
{
varr=this.match(/^(\d{1,4})(-\/)(\d{1,2})\2(\d{1,2})(\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if(r==null)returnfalse;vard=newDate(r[1],r[3]-1,r[4],r[5],r[6],r[7]);
return(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
}
alert(2002-1-3112:34:56.isTime());
alert(2001-2-2912:54:56.isTime());
alert(2002-1-4112:00:00.isTime());
</script>
文章标题:JavaScript判断两种格式的输入日期的正确性的代码
文章链接:http://soscw.com/index.php/essay/18685.html