javascript force date into input field
i have 2 input fields for a check in date and a check out date. when the user puts in the dates it calculates the nights. However i dont want the user to be able to put a date 30 days after the check in date. Ive used an alert to bring up a message if nights is greater then 30 however the date u selected goes into the check out date. Im trying to use innerHTML to force the date in the check out to be what i want i开发者_StackOverflow社区t to be ie 1 day after the check in date if they have selected more then 30 days. Heres part of my code.
function DoDepart() {
Date.fromUKFormat = function(sUK)
{
var A = sUK.split(/[\\\/]/);
A = [A[1],A[0],A[2]];
return new Date(Date.parse(A.join('/')));
}
var a = Date.fromUKFormat(document.getElementById('datepicker').value);
var b = Date.fromUKFormat(document.getElementById('departure').value);
var x = ((b - a) / (24*60*60*1000));
if (x > 30)
{
alert("check out date must be within 30 days of your check in date");
document.getElementById('departure').innerHTML = 'hey';<!--this bit must be wrong
}
document.getElementById('n').value = x;
};
any help would be appreciated
If #departure is a text input element, you should edit their value this way:
document.getElementById('departure').value= 'hey';
精彩评论