救命啊,一百分相送

xgy_p 2002-08-13 06:42:26
是关于用javascript检验数据的正确性的。一个文本框,让用户填入时间,格式为hh:mm(24小时制),比如20:09即是正确的时间,而89:10,10:78均是不正确的。中间只能用:分隔,也不能用-分隔。请问各位高手,应该如何检验出这个时间的正确与否。谁给出一个检验函数,如果能用的话,立即一百分相送。不够还可以加。
...全文
18 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
seabell 2002-08-20
  • 打赏
  • 举报
回复
休息了一周,今天才回来:)
lisi(李四):多谢你的提示!修改如下
其实qiushuiwuhen(秋水无恨)用正则表达式方法比我好了
<script>
function check(obj)
{
mytime=obj.value.split(":");
if(mytime.length!=2)
alert("pls with hh:mm!");
else
if(isNaN(mytime[0]))
alert("hours wrong!");
else
if(isNaN(mytime[1]))
alert("minutes wrong!");
else
if(mytime[0]>23||mytime[0]<0||mytime[0].length>2||mytime[0].substring(0,1)=="-")
alert("hours wrong!");
else if(mytime[1]>59||mytime[1]<0||mytime[1].length>2||mytime[1].substring(0,1)=="-")
alert("minutes wrong!");
else
alert("right!");
}
</script>
<input name=hhmm onblur=check(this)>
richardluopeng 2002-08-14
  • 打赏
  • 举报
回复
学习
shitalone 2002-08-14
  • 打赏
  • 举报
回复
str.match(/^(\d{1,2}):(\d{1,2})$/)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
match里面用的是不是叫正则表达式,能否详细说一下各个符号代表何意思。
^是否表示开头,$是否表示结尾。
qiushuiwuhen 2002-08-14
  • 打赏
  • 举报
回复
那是故意添上去的,如果要求都要两位
var arr=str.match(/^(\d{1,2}):(\d{1,2})$/)
=>
var arr=str.match(/^(\d{2}):(\d{2})$/)

willway 2002-08-14
  • 打赏
  • 举报
回复
用1:1测试,right!可见hour和minute两位没有判断
lisi 2002-08-14
  • 打赏
  • 举报
回复
seabell(百合心)

我说几句::你用-0:2测试一下你的脚本,看看,是什么结果??

仅供参考,没有别的意思,
xgy_p 2002-08-13
  • 打赏
  • 举报
回复
to seabell(百合心):
我试了一下好像可以检验了啊,太谢谢你啦。我这张帖放多几天再结帐,谢谢!
seabell 2002-08-13
  • 打赏
  • 举报
回复
<script>
function check(obj)
{
mytime=obj.value.split(":");
if(mytime.length!=2)
alert("pls with hh:mm!");
else
if(isNaN(mytime[0]))
alert("hours wrong!");
else
if(isNaN(mytime[1]))
alert("minutes wrong!");
else
if(mytime[0]>23||mytime[0]<0)
alert("hours wrong!");
else if(mytime[1]>59||mytime[1]<0)
alert("minutes wrong!");
else
alert("right!");
}
</script>
<input name=hhmm onblur=check(this)>
qiushuiwuhen 2002-08-13
  • 打赏
  • 举报
回复
<script>
function checkTime(str){
var arr=str.match(/^(\d{1,2}):(\d{1,2})$/)
return (arr!=null)&&(Number(arr[1])<24)&&(Number(arr[2])<60)
}
alert(checkTime("20:09"))
alert(checkTime("89:10"))
alert(checkTime("10:78"))
alert(checkTime("aa"))
</script>

87,996

社区成员

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

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