打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
时间输入的校验 - - ItEye技术网站
旭龙
>《Javascript》
2011.04.13
关注
<html>
<head>
<title>时间输入的校验--包子剑客</title>
<!--
2009.12
.
12
包子剑客 广州-->
</head>
<body>
时间输入的校验<br/>
请输入时间: <br/>
<input type=
"text"
onblur=
"isTimeFormat(this)"
onkeyup=
"verify(this)"
onkeypress=
"return inputNumber(event,this);"
title=
"时间格式:00:00"
/>
<br/>
</body>
</html>
<script>
//以下是时间的显示相关函数
function isTimeFormat(str) {
var a = str.value.match(/^([
0
-
2
][
0
-
9
]):([w0-
5
][
0
-
9
])$/);
if
(a ==
null
) {
alert(
"时间格式不对,已经被自动修正,请留意。"
);
var length=str.value.length;
if
(length==
1
) {str.value=
"0"
+ str.value +
":00"
;}
else
if
(length==
2
) {str.value+=
":00"
;}
else
if
(length==
3
) {str.value+=
"00"
;}
else
if
(length==
4
) {str.value+=
"0"
;}
else
{str.value=
"00:00"
;}
str.select();
return
false
;
}
return
true
;
}
//格式化时间格式
function verify(text){
var hour;
var minute;
var tmp;
var index;
var textValue = text.value;
if
(textValue.length ==
1
) {
if
(textValue ==
":"
){text.value =
"00:"
; }
return
true
;
}
if
(textValue.length ==
2
) {
if
(!isNum(textValue)) text.value =
"0"
+ textValue;
return
true
;
}
if
(textValue.length >
2
){
//当长度超过2时,进行处理
hour = textValue.substr(
0
,
2
);
//取前两位数字,即小时
if
(!isNum(hour)){
//不是数字
text.value =
'00'
;
return
;
}
if
(hour <
24
){
//10<x<24
text.value = hour +
':'
;
//显示小时
index = textValue.indexOf(
':'
);
//定位冒号
minute = index >
0
? textValue.substr(index +
1
,
2
) : textValue.substr(
2
,
2
);
if
(!isNum(minute)){
//不是数字
text.value = hour +
':00'
;
return
;
}
if
(minute <
59
){
tmp = hour +
':'
+ minute;
}
else
{
tmp = hour +
':59'
;
}
}
else
{
//x>=24
hour =
'0'
+ textValue.substr(
0
,
1
);
text.value = hour +
':'
+ text.value.substr(
1
,
1
);
index = textValue.indexOf(
':'
);
minute = index >
0
? textValue.substr(index +
1
,
2
) : textValue.substr(
1
,
2
);
if
(!isNum(minute)){
//不是数字
text.value = hour +
':00'
;
return
;
}
if
(minute <
59
){
tmp = hour +
':'
+ minute;
}
else
{
tmp = hour +
':59'
;
}
}
text.value = tmp;
//输入“小时:分钟”格式
}
}
//只让输入数字和:
function inputNumber(e,textValue){
var keynum;
var keychar;
var numcheck;
if
(window.event)
// IE
{
if
(
58
==e.keyCode) {
return
true
;}
keynum = e.keyCode
}
else
if
(e.which)
// Netscape/Firefox/Opera
{
keynum = e.which
}
keychar = String.fromCharCode(keynum);
return
isNum(keychar);
}
//是否为数字
function isNum(str){
if
(
""
==str){
return
true
;
}
var reg = /\D/;
return
str.match(reg)==
null
;
}
//以上是时间的显示相关函数
</script>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
JavaScript常用检测脚本
如何用js完美的解析lrc歌词
JS 格式化数据
public.js 银行通用方法
js中用gb2312编码解码
Javascript面试笔试题(考试时间90分钟)前端开发的可以试一下!
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×