[code="java"][/code]
<!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 [1]>24 || time[3]>60 || time[4]>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[1]+":"+time[3]+" - "+time[4]+":"+time[6]);
if (time[1]>24 || time[3]>60 || time[4]>24 ||time[6]>60)
{
alert("时间格式不对");
return false
}
if ((time[1]+time[3])>(time[4]+time[6]))
{
alert('结束时间不能早于开始时间');
return false;
}
return true;
}
</script>
</head>
<body>
<input type="text" id="times">
<button οnclick="javascript:isTimeFormat();"> 测试时间格式</a>
</body>
</html>
<!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 [1]>24 || time[3]>60 || time[4]>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[1]+":"+time[3]+" - "+time[4]+":"+time[6]);
if (time[1]>24 || time[3]>60 || time[4]>24 ||time[6]>60)
{
alert("时间格式不对");
return false
}
if ((time[1]+time[3])>(time[4]+time[6]))
{
alert('结束时间不能早于开始时间');
return false;
}
return true;
}
</script>
</head>
<body>
<input type="text" id="times">
<button οnclick="javascript:isTimeFormat();"> 测试时间格式</a>
</body>
</html>