标签:str style test rip font event nbsp alert logs
正则表达式之时间验证
正则表达式:
var reg =
/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)
(\T)
(([0-1]{1}[0-9]{1})|([2]{1}[0-4]{1}))
([:])
(([0-5]{1}[0-9]{1}|[6]{1}[0]{1}))
([:])
([0-5]{1}[0-9]{1}|[6]{1}[0]{1})$/
验证格式:
2080-02-10T12:60:30
验证demo:
<html> <script> function IsDate() { //输入的日期 var reg = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)(\T)(([0-1]{1}[0-9]{1})|([2]{1}[0-4]{1}))([:])(([0-5]{1}[0-9]{1}|[6]{1}[0]{1}))([:])([0-5]{1}[0-9]{1}|[6]{1}[0]{1})$/ var str = document.getElementById("date").value; var arr = reg.test(str); if (str == "") { alert("请输入日期"); } else if(!arr){ alert("请输入正确的日期格式,例:2080-02-10T12:60:30"); }else{ alert("日期格式正确"); } } </script> <body> 输入您要验证的日期:<input type="text" id="date" name="date" /> <br /> <br /> <input type="button" id="btnValid" value="验证日期格式" onclick="IsDate()"/> </body> </html>
测试用例:
2012-04-20T12:12:12 √ 2012-12-20T12:12:12 √ 2012-13-20T12:12:12 × (判断一年多少个月) 2012-04-30T12:12:12 √ 2012-04-32T12:12:12 × (判断一个月多少天) 2000-2-29T12:60:30 × 2000-02-29T12:60:30 √ 2000-02-30T12:12:12 × (判断平年闰年) 2017-12-25T12:23:23 √ 2017-12-25T24:23:23 √ 2017-12-25T26:23:23 × (判断小时) 2017-12-25T12:59:23 √ 2017-12-25T24:60:23 √ 2017-12-25T12:62:23 × (判断分钟) 2017-12-25T12:59:59 √ 2017-12-25T24:60:60 √ 2017-12-25T12:62:63 × (判断秒)
资料来源:杰儿~~
标签:str style test rip font event nbsp alert logs
原文地址:http://www.cnblogs.com/s313139232/p/7772166.html