最简单的
function isValidDate(dateStr) {
var matchArray = dateStr.match(/^[0-9]+-[0-1][0-9]-[0-3][0-9]$/)
if (matchArray == null) {
alert("Invalid date: " + dateStr);
return false;
}
return true;
}
function isValidDate(dateStr) {
var matchArray = dateStr.match(/^[0-9]+-[0-1][0-9]-[0-3][0-9]$/)
if (matchArray == null) {
alert("Invalid date: " + dateStr);
return false;
}
return true;
}
第二种
<script> <BR>String.prototype.isDate = function() <BR>{ <BR> var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); <BR> if(r==null)return false; var d = new Date(r[1], r[3]-1, r[4]); <BR> return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]); <BR>} <BR>alert("2002-01-31".isDate()); <BR>alert("2002-01-41".isDate()); <BR></script>
<script> <BR>String.prototype.isDate = function() <BR>{ <BR> var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); <BR> if(r==null)return false; var d = new Date(r[1], r[3]-1, r[4]); <BR> return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]); <BR>} <BR>alert("2002-01-31".isDate()); <BR>alert("2002-01-41".isDate()); <BR></script>
第三种,比较复杂的
<script> <BR>String.prototype.isTime = function() <BR>{ <BR> var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/); <BR> if(r==null)return false; var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); <BR> return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]); <BR>} <BR>alert("2002-1-31 12:34:56".isTime()); <BR>alert("2001-2-29 12:54:56".isTime()); <BR>alert("2002-1-41 12:00:00".isTime()); <BR></script>
Kenyataan:Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn