[js]判斷日期不能超過今天
在做會員註冊時、常常會判斷生日不能超過今天的日期
實際作法如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<title>Datepicker範例</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7/themes/redmond/jquery-ui.css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7/jquery-ui.min.js"></script>
<script type="text/javascript">
var DatePicked = function() {
var birthday = $("#birthday");
var birthdayDate = birthday.datepicker("getDate");
var nowDate = new Date();
if (birthdayDate != null && birthdayDate > nowDate) {
birthday.datepicker("setDate", '');
alert("生日不能超過今天的日期!!");
}
}
$(function() {
$("#birthday").datepicker({
onSelect: DatePicked,
dateFormat: "yy-mm-dd",
yearRange: "-100:+0",
changeMonth: true,
changeYear: true
});
});
</script>
</head>
<body>
<form id="myform" name="myform" method="post">
<input type="text" id="birthday" name="birthday" />
<input type="submit">送出
</form>
留言列表