unikwu 发表于 2013-2-7 16:57:36

Javascript时间格式校验

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>TestTime.html</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function isTimeFormat()
{
   var str = document.getElementById('times').value;
   /*精确到秒
   var time = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
   if (time == null) {alert('输入的参数不是时间格式'); return false;}
   if (time >24 || time>60 || time>60)
   {
       alert("时间格式不对");
       return false
    }*/   
    //精确到分(时段12:20-14:50)
    if(str==""){
    alert('请求时段不能为空');
    return false;
    };
    var time = str.match(/^(\d{1,2})(:)?(\d{1,2})-(\d{1,2})(:)?(\d{1,2})$/);
    if (time == null){
    alert('输入的参数不是有效的请求时段');
    return false;
    }
    //alert(time+":"+time+" - "+time+":"+time);
   if (time>24 || time>60 || time>24 ||time>60)
   {
       alert("时间格式不对");
       return false
    }
    if ((time+time)>(time+time))
    {
    alert('结束时间不能早于开始时间');
    return false;
    }
    return true;
}
</script>
</head>

<body>
    <input type="text" id="times">
    <button > 测试时间格式</a>
</body>
</html>
页: [1]
查看完整版本: Javascript时间格式校验