<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>
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>
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)>
我试了一下好像可以检验了啊,太谢谢你啦。我这张帖放多几天再结帐,谢谢!
var arr=str.match(/^(\d{1,2}):(\d{1,2})$/)
=>
var arr=str.match(/^(\d{2}):(\d{2})$/)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
match里面用的是不是叫正则表达式,能否详细说一下各个符号代表何意思。
^是否表示开头,$是否表示结尾。
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)>