I want a JavaScript that restricts user to enter a date which is minimum 5 days after today date .I've written js for getting todays date but i can't figure out how to change 'min' value dynamically.Here,I've entered constant value for min as 2016-02-14.how to make this as variable ?
<input type='date' id='d1' name='d1' min="2016-02-14">
<script type="text/javascript">
var d1 = new Date();
var y1= d1.getFullYear();
var m1 = d1.getMonth()+1;
if(m1<10)
m1="0"+m1;
var dt1 = d1.getDate();
if(dt1<10)
dt1 = "0"+dt1;
var d2 = y1+"-"+m1+"-"+dt1;
document.getElementById('d1').value=d2;
</script>
答案 0 :(得分:0)
试试这个 - 不确定它是否适用于所有浏览器 - firefox和IE可能不支持日期/时间输入的最小值和最大值。可能更好地使用日期时间或时间戳并将其用作数字(可以很容易地设置为最小值)并修改输入。
<!DOCTYPE html>
<html>
<head>
<title>Date</title>
<meta charset="utf-8">
</head>
<body>
<form>
<input type='date' id='d1' name='d1' min="">
</form>
<script>
var d1 = new Date();
var y1= d1.getFullYear();
var m1 = d1.getMonth()+1;
if(m1<10){ m1="0"+m1};
var dt1 = d1.getDate();
if(dt1<10){dt1 = "0"+dt1};
var d2 = y1+"-"+m1+"-"+dt1;
document.getElementById('d1').value=d2;
var future = new Date();
future.setDate(future.getDate() + 5);
var futureYear= future.getFullYear();
var futureMonth = future.getMonth()+1;
if(futureMonth<10){futureMonth="0"+futureMonth};
var futureDay = future.getDate();
if(futureDay<10){futureDay = "0"+futureDay};
var futureDate = futureYear+"-"+futureMonth+"-"+futureDay;
document.getElementById('d1').setAttribute("min", futureDate);
</script>
</body>
</html>