求助,如何验证这样的输入日期

aaaaaa2002 2003-07-22 07:33:02
请问那位网友遇到过这样的问题?在一个表单中验证一个输入框的输入信息为
日期型,它的输入格式必须是(DD-MON-YYYY)或者(MON-YYYY)。
...全文
29 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzt2000 2003-07-23
  • 打赏
  • 举报
回复
ky99xing(kk)大侠帮帮忙~!
我正在找一个简单实用的拖动式日历控件,可否发一个给我,
wzt2000@163.net
谢谢!!
fzel_net 2003-07-23
  • 打赏
  • 举报
回复
正规表达式
xlbh 2003-07-23
  • 打赏
  • 举报
回复
asp 中 不是可以用isdate()来校验的么?
ky99xing 2003-07-23
  • 打赏
  • 举报
回复
可以用下拉的日历输入,源代码要的话跟我联系:ky99xing@163.com

或者用下面的函数检验,日期格式改成你要的


////////////////////////////////////////////////////////
//检查输入的时间是否符合指定的格式,格式改为你要的//
///////////////////////////////////////////////////////
function checkT(strTC)
{
flg=0;
temp="";
for (var i=0;i<strTC.length;i++)
{
cmp="0123456789-";
tst=strTC.substring(i,i+1);
temp = temp + "*" + tst;
if (cmp.indexOf(tst)<0)
{
flg++;
}
}
if (flg!=0)
{
return(false);
}
return(true);
}

function valDate(M, D, Y)
{
Months= new Array(31,28,31,30,31,30,31,31,30,31,30,31);
Leap = false;

if((Y % 4 == 0) && ((Y % 100 != 0) || (Y %400 == 0))) Leap = true;
if((D < 1) || (D > 31) || (M < 1) || (M > 12) || (Y < 0)) return(false);
if((D > Months[M-1]) && !((M == 2) && (D > 28))) return(false);
if(!(Leap) && (M == 2) && (D > 28)) return(false);
if((Leap) && (M == 2) && (D > 29)) return(false);
}

function formatDate(dateField)
{
cDate = dateField.value;
dSize = cDate.length;
aCount = 0;
bCount = 0;
cCount = 0;

if(cDate=='' || dSize==0)
{
alert("请输入日期!");
dateField.focus();
return(false);
}
if(!checkT(cDate))
{
alert("输入的日期格式必须是:年-月-日\n例如:2003-1-1");
dateField.focus();
return(false);
}
for(var i=0; i < dSize; i++)
{
(cDate.substr(i,1) == "-") ? aCount++ : aCount;
}
if ((aCount != 2))
{
alert("输入的日期格式必须是:年-月-日\n例如:2003-1-1");
dateField.focus();
return(false);
}

//将输入的日期字符串分隔成3部分 (Month, Day & Year
idxBar1 = cDate.indexOf("-");
idxBar2= cDate.lastIndexOf("-");
strM = cDate.substring(idxBar1+1,idxBar2);
strD = cDate.substring(idxBar2+1,dSize);
strY = cDate.substring(0,idxBar1);
//alert("strY = "+strY+"\nstrM = "+strM+"\nstrD = "+strD);

//检测输入的年份是2位数还是4位数;
ySize = strY.length
if(ySize!=2 && ySize!=4 )
{
alert('您输入的日期错误 !');
dateField.focus();
return false;
}

strM = (strM.length < 2 ? '0'+strM : strM);
strD = (strD.length < 2 ? '0'+strD : strD);
if(strY.length == 2) strY = (strY > 50 ? '19'+strY : '20'+strY);
dateField.value = strY+'-'+strM+'-'+strD

ok = valDate(strM, strD, strY);
if(ok==false)
{
alert("您输入的日期错误 !");
dateField.focus();
return false;
}
}
iinohk 2003-07-23
  • 打赏
  • 举报
回复
一定要這樣做才可以嗎?
現在不少人都用select來限制client所輸入的資料矣
既省時間而且不用太多check case
新手拙見,請勿見笑
形印声传 2003-07-22
  • 打赏
  • 举报
回复
关注~~

87,988

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧