日期:2014-05-16 浏览次数:20531 次
<input type="text" id="time" onchange="vTime(this.value);" /> <script type="text/javascript"> function vTime(s) { var ar = s.split(':'), FLAG = true; if (ar.length != 2) FLAG = false; else { if (!/^[\d]+$/.test(ar[0]) || !/^[\d]+$/.test(ar[1])) FLAG = false; else { if (parseInt(ar[0]) < 0 || parseInt(ar[0]) > 23 || parseInt(ar[1]) < 0 || parseInt(ar[1]) > 59) FLAG = false; } } if (!FLAG) { alert('时间格式有误'); return false; } } </script>
------解决方案--------------------
var reg=/^([01]?[0-9]|2[0-4]):[0-6][0-9]$/;
var a="8:30";
alert(reg.test(a));
a="09:30";
alert(reg.test(a));
a="17:30";
alert(reg.test(a));
这样试试看可以不
------解决方案--------------------
var data = [ '9:45', '19:45', '0:0', '9:55', '09:55', '25:00', '24:00', '23:60', '23:59', '18:59' ]; var re = /^(?:0?\d|1\d|2[123]):[012345]?\d$/; for(var i = 0, len = data.length; i < len; i++){ document.write( data[i] + ' ------------ '+ re.test(data[i]) + '<br />' ) }
------解决方案--------------------
^(?:[01]?\d|2[0-3]):(?:[0-5]?\d)$