网页中有一文本框,如何约束用户输入格式

lyf123 2001-07-19 11:00:31
网页中有一文本框,如何约束用户输入格式。比如我只允许用户按照YYYY-MM-DD格式输入日期
...全文
72 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hydnoahark 2001-07-19
  • 打赏
  • 举报
回复
不如用户日历控件(ocx的或js/vbs的)
freezwy 2001-07-19
  • 打赏
  • 举报
回复
对了,最好在客户端检查的函数全部写成一个JS然后在需要的地方引用就是了。
freezwy 2001-07-19
  • 打赏
  • 举报
回复
客户端检查,给你段我的代码,给分!
<script language=javascript>
function parseYMD(theYear,theMonth,theDay) {
theYear=parseNum(theYear)
theMonth=parseNum(theMonth)
theDay=parseNum(theDay)
if ((theYear < 1900) || (theYear > 3000)){
return 1
}
if (theMonth < 1 || theMonth > 12){
return 2
}
if ((theMonth==1 || theMonth==3 || theMonth==5 || theMonth==7 || theMonth==8 || theMonth==10 || theMonth==12) &&
(theDay <1 || theDay > 31)
){
return 3
}
if ((theMonth==4 || theMonth==6 || theMonth==9 || theMonth==11) &&
(theDay <1 || theDay > 30)
){
return 3
}
if (theYear%400==0 || (theYear%4==0 && theYear%100!=0)){ //闰年
if (theMonth==2 && (theDay <1 || theDay > 29) )
return 3
}
else //平年
if (theMonth==2 && (theDay <1 || theDay > 28) )
return 3
return 0
}
function isInvalidDate(theDate,separator){
default_style=1
if (theDate.length>10 || theDate.length<8)
return true
idx1=theDate.indexOf(separator)
if (idx1==-1)
return true
idx2=theDate.indexOf(separator,idx1+1)
if (idx2==-1)
return true
if (isInvalidDate.arguments.length>2)
default_style=isInvalidDate.arguments[2]
if (default_style<1 || default_style>9){
alert("传入参数有误!请检查。")
return true
}
if (default_style==1){
theYear=theDate.substring(0,idx1)
theMonth=theDate.substring(idx1+1,idx2)
theDay=theDate.substring(idx2+1)
}
if (default_style==2){
theMonth=theDate.substring(0,idx1)
theDay=theDate.substring(idx1+1,idx2)
theYear=theDate.substring(idx2+1)
}
if (theDay.length>2)
return true
if (parseYMD(theYear,theMonth,theDay)>0)
return true
else
return false
}
function checkinput()
{
if (isInvalidDate(document.regform.birthday.value,"-"))
{
alert("您的出生年月填写错误,格式是2001-6-1!");
document.regform.birthday.focus();
return false;
}
return true;

}
</script>
调用就可以了。
<form name=regform action=xx.asp method=post onsubmit="return checkinput();">
....
</form>
够用了吧。

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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