/    Sign up×
Community /Pin to ProfileBookmark

Date Range box with today’s date

I have six drop-down boxes for hotel check-in dates:

check-in: MMM DD YYYY
check-out: MMM DD YYYY

Anyone know where I can get some JS code to be able to populate a drop-down date for check-in with today’s date AND also have it work so that it forces the check-out date to come AFTER the check-in date, and if possible, validates dates, so that Feb. 31 never shows up.

Thx,
Zeb

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@CharlesFeb 04.2003 — [font=georgia]Keep in mind that JavaScript will fail at least one in ten times so if this validation is at all important then you will need some server side redundancy. In certain places this is required by accessibility laws. But this might be a start:[/font]

[font=monospace]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>form example</title>

<script type="text/javascript">

<!--

Number.prototype.toPadded = function (p) {var n = Number(this); var s = ''; if (/^0+$/.test(p) && n < 0) {s = '-'; n = Math.abs(n)}; return s + p.slice(0, 0 - n.toString().match(/^[^.]*/)[0].length) + n.toString()}

Date.prototype.toString = function () {return [['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jne', 'Jly', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][this.getMonth()], this.getDate().toPadded('00'), this.getFullYear()].join(' ')};

window.onload = function () {document.forms[0].checkIn.value = new Date()}

function handleForm (f) {

out = new Date(f.checkOut.value);

now = new Date();

if (out < now) {alert ('That doesn't seem to work.'); f.checkOut.value = ''; f.checkout.focus(); return false};

}

// -->

</script>

<form action="handler.pl" onsubmit="return handleForm(this)">

<p><label for="checkIn">Check in:</label><br><input type="text" name="checkIn"onchange="this.value= new Date(this.value)"></p>

<p><label for="checkOut">Check out:</label><br><input type="text" name="checkOut" onchange="this.value= new Date(this.value)"></p>

<p><input type="submit"></p>

</form>

[/font]
×

Success!

Help @aagha spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...