<!DOCTYPE html>
<html>
<head>
<title>js.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function valid(){
var date1 = document.getElementById("d1").value;
var date2 = document.getElementById("d2").value;
if(date1 != ""){
//验证日期是否合法
if(!valid(date1)){
alert("日期不合法");
return false;
}else{
alert("正确的日期");
}
}
if(date2 != ""){
//验证日期是否合法
if(!valid(date2)){
return false;
}else{
alert("正确的日期");
}
}
if(date1 != "" && date2 != ""){
return compareDate(date1,date2);
}
function compareDate(date1,date2){
var arr1 = date1.split("-");
var arr2 = date2.split("-");
var d1 = new Date(arr1[0],arr1[1],arr1[2]);
var d2 = new Date(arr2[0],arr2[1],arr2[2]);
if(d1 > d2){
alert("前一个日期大于后一个");
return true;
}
alert("前一个日期小于后一个");
return false;
}
function valid(date){
//格式2016-12-31
var zhengze = /\d{4}-(0?[0-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])/;
return zhengze.test(date);
}
/*if(date2 != ""){
//验证日期是否合法
alert(date2);
if(!valid(date2)){
return false;
}else{
alert("正确的日期");
}
}
if(date1 != "" && date2 != null){
if(compareDate(date1,date2)){
alert("大于");
return true;
}else{
alert("小于");
return false;
}
}else{
alert("日期不能为空");
}
function valid(date){
//格式2016-12-31
var zhengze = /\d{4}-(0?[0-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])/;
return zhengze.test(date);
}
function compareDate(date1,date2);{
var date1Array = date1.split("-");
var date2Array = date2.split("-");
alert(date1Array+"::"+date2Array);
var d1 = new Date(date1Array[0],date1Array[1],date1Array[2]);
var d2 = new Date(date2Array[0],date2Array[1],date2Array[2]);
if(d1 > d2){
return true;
}
} */
}
</script>
</head>
<body>
<form id="form1" action="#" >
<input type="text" id="d1"/><br>
<input type="text" id="d2"/><br>
<input type="button" value="验证" onclick="valid()"></input>
</form>
</body>
</html>