请教:如何用Javascript判断日期格式是否正确? if (isNaN(Date.parse("MM/DD/YYYY"))){}if (isNaN(Date.UTC(year, month, day))) {} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 还有不太正统的做法:if ((Date.parse("MM/DD/YYYY")+"")=="NaN" ){}或if ((Date.UTC(year, month, day))+"")=="NaN") {} 呵呵,楼上的方法很简单,也很实用。我这有段代码,可以判断用户的输入是否符合日期格式。你稍做改动,就可以使用了:function verifyDate(textObj){ var tmpDateValue = textObj.value; var tmpLength = tmpDateValue.length; if (tmpLength == 0){ return true; } for (var i = 0; i < tmpLength;i++){ aChar = tmpDateValue.substring(i,i+1); if(aChar != "-" && (aChar < "0" || aChar > "9")) { alert ("请按照格式输入日期(yyyy-mm-dd)。"); textObj.focus(this); textObj.select(this); return false; } } if ((tmpLength < 8 || tmpLength > 10) && tmpLength != 0) { alert ("请按照格式输入日期(yyyy-mm-dd)。"); textObj.focus(this); textObj.select(this); return false; } for (var j= 0; j < 4;j++){ aChar = tmpDateValue.substring(j,j+1); if(aChar < "0" || aChar > "9") { alert ("请按照格式输入日期(yyyy-mm-dd)。"); textObj.focus(this); textObj.select(this); return false; } } if (tmpDateValue.substring(4,5) != "-" || tmpDateValue.substring(5,6) == "-"){ alert ("请按规定格式输入日期(yyyy-mm-dd)。"); textObj.focus(this); textObj.select(this); return false; } if (tmpLength == 8){ if (tmpDateValue.substring(6,7) != "-" || tmpDateValue.substring(7,8) == "-" ){ alert ("请按规定格式输入日期(yyyy-mm-dd)。"); textObj.focus(this); textObj.select(this); return false; } } if (tmpLength == 9){ if (tmpDateValue.substring(8,9) == "-" ){ alert ("请按规定格式输入日期(yyyy-mm-dd)。"); textObj.focus(this); textObj.select(this); return false; } } if (tmpLength == 10){ if (tmpDateValue.substring(7,8) != "-" || tmpDateValue.substring(6,7) == "-" || tmpDateValue.substring(8,9) == "-" || tmpDateValue.substring(9,10) == "-" ){ alert ("请按规定格式输入日期(yyyy-mm-dd)。"); textObj.focus(this); textObj.select(this); return false; } } var count=0; for (var k = 0; k < tmpLength;k++){ aChar = tmpDateValue.substring(k,k+1); if(aChar == "-") { count++; } } if (count!=2){ alert("请按照格式输入日期!(yyyy-mm-dd)"); textObj.focus(this); textObj.select(this); return false; } return true;} 我觉得用户界面应该友好些把年月日给用户选择<select>那样出错的机会会小很多。。至少格式上不会出错。。 谢谢chenzengxi(懒猫)与vincentmax(天地任逍遥),由其是chenzengxi(懒猫)方法,我就陷进NaN这个旋涡之中,vincentmax(天地任逍遥)方法我也考虑过,但是太麻烦,不太实用。不过对于linhaibo(美洲豹),有人用select选择2-30日期不也是错误的吗? 正则表达式求 求详解求详解] 运用别人的程序,但本地 打开和上传到服务器后都不能播放mp3 js实现页面跳转的一个问题 没有调用去执行iframe的方法,去自动跳转执行了 大哥大姐们帮忙啊 IE6动态给select标签添加多选属性无效. 急,为什么这个脚本不好用了 如何打印出form 表单 ? 在线等JS函数问题 怎样使一个form中的select选单发生变化时一个text表单的值也变。 大家帮忙!请问我下面这段代码错在那里了!谢谢! 用Microsoft Visual InterDev 6.0编写关于xml的js的奇怪问题! 如何用程序生成一个本地机器上一个类的实例??
if ((Date.parse("MM/DD/YYYY")+"")=="NaN" ){}
或
if ((Date.UTC(year, month, day))+"")=="NaN") {}
var tmpDateValue = textObj.value;
var tmpLength = tmpDateValue.length;
if (tmpLength == 0){
return true;
}
for (var i = 0; i < tmpLength;i++){
aChar = tmpDateValue.substring(i,i+1);
if(aChar != "-" && (aChar < "0" || aChar > "9")) {
alert ("请按照格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
if ((tmpLength < 8 || tmpLength > 10) && tmpLength != 0) {
alert ("请按照格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
for (var j= 0; j < 4;j++){
aChar = tmpDateValue.substring(j,j+1);
if(aChar < "0" || aChar > "9") {
alert ("请按照格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
if (tmpDateValue.substring(4,5) != "-" || tmpDateValue.substring(5,6) == "-"){
alert ("请按规定格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
if (tmpLength == 8){
if (tmpDateValue.substring(6,7) != "-" || tmpDateValue.substring(7,8) == "-" ){
alert ("请按规定格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
if (tmpLength == 9){
if (tmpDateValue.substring(8,9) == "-" ){
alert ("请按规定格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
if (tmpLength == 10){
if (tmpDateValue.substring(7,8) != "-" || tmpDateValue.substring(6,7) == "-" || tmpDateValue.substring(8,9) == "-" || tmpDateValue.substring(9,10) == "-" ){
alert ("请按规定格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
var count=0;
for (var k = 0; k < tmpLength;k++){
aChar = tmpDateValue.substring(k,k+1);
if(aChar == "-") {
count++;
}
}
if (count!=2){
alert("请按照格式输入日期!(yyyy-mm-dd)");
textObj.focus(this);
textObj.select(this);
return false;
}
return true;
}
把年月日给用户选择<select>那样出错的机会会小很多。。至少格式上不会出错。。