<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>

解决方案 »

  1.   

    <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)>
      

  2.   

    to seabell(百合心):
          我试了一下好像可以检验了啊,太谢谢你啦。我这张帖放多几天再结帐,谢谢!
      

  3.   

    seabell(百合心) 我说几句::你用-0:2测试一下你的脚本,看看,是什么结果??仅供参考,没有别的意思,
      

  4.   

    用1:1测试,right!可见hour和minute两位没有判断
      

  5.   

    那是故意添上去的,如果要求都要两位
    var arr=str.match(/^(\d{1,2}):(\d{1,2})$/)
    =>
    var arr=str.match(/^(\d{2}):(\d{2})$/)
      

  6.   

    str.match(/^(\d{1,2}):(\d{1,2})$/)
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    match里面用的是不是叫正则表达式,能否详细说一下各个符号代表何意思。
    ^是否表示开头,$是否表示结尾。
      

  7.   

    休息了一周,今天才回来:)
    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)>