<script language=javascript> function strDateTime(str){ var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; var r = str.match(reg); if(r==null)return false; var d= new Date(r[1], r[3]-1,r[4]); var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate() return newStr==str } alert(strDateTime("2002-1-31")) alert(strDateTime("2002-1-41")) </script> <script language=javascript> function strDateTime(str){ var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; var r = str.match(reg); if(r==null)return false; var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds() return newStr==str } alert(strDateTime("2002-1-31 12:34:56")) alert(strDateTime("2001-2-29 12:54:56")) alert(strDateTime("2002-1-41 12:00:00")) </script>
<script language="javascript"> var s="2002-4-16" alert(chkDate(s)); function chkDate(sDate){ var r=/\d{4}(?:-\d{1,2}){0,2}/ //正则表达式,判断是否为yyyy-mm-dd,yyyy-mm,yyyy格式 if(sDate.match(r)==sDate){ var arr=sDate.split("-") switch(arr.length){ //根据不同的yyyy-mm-dd,yyyy-mm格式判断年月日数字是否正确 case 3: var tmpDate=new Date(arr[0],arr[1],arr[2]); if(tmpDate.getMonth()==arr[1] && tmpDate.getFullYear()==arr[0]) return true; break; case 2: if(arr[1]<13) return true; break; default: return false; } } return false; } </script>
/* 时间有效性判断函数 All by happywinds */ function verifyDate(textObj) { var str=textObj.value; textObj.value = textObj.value.replace(/\s+/g,""); if(str.search(/^\d{4}-\d{1,2}-\d{1,2}$/) == 0){ var y = parseInt(str.split("-")[0]); var m = parseInt(str.split("-")[1]); var d = parseInt(str.split("-")[2]); switch(m){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: if(d>31){ return false; textObj.focus(); textObj.select(); }else{ return true; } break; case 2: if((y%4==0 && d>29) || ((y%4!=0 && d>28))){ return false; textObj.focus(); textObj.select(); }else{ return true; } break; case 4: case 6: case 9: case 11: if(d>30){ return false; textObj.focus(); textObj.select(); }else{ return true; } break; default: return false; textObj.focus(); textObj.select(); } }else{ return false; textObj.focus(); textObj.select(); } }
[1] |
|