Last updated:
This article shows you how to use JavaScript to create a booking form for a holiday let. Users enter their arrival and departure dates. The form checks that the dates make sense, works out the number of nights and shows the total price straight away.
The example uses the following prices:
At the end of the article, you'll also find out how to set different prices for peak and off-peak season.
When you work with dates in JavaScript, you'll almost always run into the same pitfalls. This guide shows you how to deal with each of them:
10/07/2027 isn't a date but a string of characters. Before you can calculate with it or compare it, you need to convert it into a date.09/08/2027 comes before 10/07/2027, because 0 comes before 1. Comparisons only work reliably with real date values.0 and December is month 11. If you forget this, you'll end up a month out.
The form has two pages: page 1 contains the travel dates and price summary, and page 2 the contact details. Make sure you use the technical names exactly as shown here, as the scripts rely on them.
Add a Text paragraph with the heading Your stay. Below it, create two input fields and mark both as Mandatory field.
Input field for the arrival date
| Setting | Value |
| Label | Arrival date |
| Technical name | arrival |
| Date format | dd/MM/yyyy |
| Mandatory field | enabled |
Input field for the departure date
| Setting | Value |
| Label | Departure date |
| Technical name | departure |
| Date format | dd/MM/yyyy |
| Mandatory field | enabled |
Below the input fields, add a Text paragraph with the heading Your price. This is followed by two Calculated values: one for the number of nights and one for the total price. You'll find the code for these in the section Calculating the nights and price.
On page 2, add the fields you need for the booking – for example, name, email address and phone number. These fields don't need any JavaScript.
The mandatory fields ensure that users enter both an arrival and a departure date. To check that the dates actually make sense together, use a validation script. It runs as soon as users move on to the next page.
What the code does
toDate() converts the text entered into a real date. If the text isn't a valid date, the function returns null.(end - start) gives the difference in milliseconds. Dividing it by 86400000 (the number of milliseconds in a day) gives you the number of nights. Math.round() compensates for the clocks changing.
To let users see the price while they're still entering their dates, create two Calculated values. The second builds on the first: the total price uses the number of nights that has already been calculated.
Enable the following options for both calculated values:
| Setting | Value |
| Label | Number of nights |
| Technical name | nights |
| JavaScript | See the code block below |
What the function does
5 nights.| Setting | Value |
| Label | Total price |
| Technical name | totalprice |
| JavaScript | See the code block below |
What the function does
nights calculated value. parseInt() only takes the number at the start, so 5 nights becomes 5.
Example: An arrival date of 10/07/2027 and a departure date of 15/07/2027 gives 5 nights and a total price of €535.00 (5 × €95.00 + €60.00). |
Many holiday lets charge more in peak season. In the following example, a night in July or August costs €120.00 and €95.00 at all other times. Because a stay can span both seasons, "nights × price" is no longer enough. Instead, the script goes through each night individually and adds the price that applies.
To do this, replace the code in the totalprice calculated value with the following:
What the function does
setDate(night.getDate() + 1) moves forward exactly one calendar day, including across the end of a month or year.Example: An arrival date of 29/06/2027 and a departure date of 03/07/2027 gives 2 off-peak nights and 2 peak nights: 2 × €95.00 + 2 × €120.00 + €60.00 = €490.00. |
| Problem | Cause | Solution |
| Nothing is displayed |
The technical name in the script doesn't match the form element, e.g. Arrival instead of arrival |
Compare the technical names, paying attention to upper and lower case |
| The display doesn't update | Recalculate value directly in browser isn't enabled | Enable this option for both calculated values |
| Every date is reported as invalid | The input field uses a date format other than dd/MM/yyyy | Select the dd/MM/yyyy date format |
| The date is a month out | The month was passed without - 1 |
Subtract 1 from the month when creating the date |
The number of nights has decimals, e.g. 4.958333 |
A stay spanning a clock change was calculated without rounding | Round the result using Math.round() |
| Dates are compared incorrectly |
The values were compared as text rather than as dates | Convert the values using toDate() first |
| Error message such as "toDate is not defined" | The helper function is missing from this script | Include toDate() in every script that uses it |
| The number of nights is shown but the total price is empty | The technical name nights is wrong in the total price script |
Check the technical name of the first calculated value |
Once you've set everything up, test the following scenarios:
| Arrival date | Departure date | Expected result |
| 10/07/2027 | 15/07/2027 | 5 nights, €535.00 |
| 10/07/2027 | 10/07/2027 | Error message: departure must be after arrival |
| 10/07/2027 | 12/07/2027 | Error message: minimum stay of 3 nights |
| A date in the past | Any date | Error message: arrival date in the past |
| 26/03/2027 | 31/03/2027 | 5 nights, €535.00 (stay spans the clocks going forward) |
| 29/06/2027 | 03/07/2027 | With seasonal pricing: 4 nights, €490.00 |
You should also submit a test booking and check that the number of nights and the total price are included in the submitted form data.
Your booking form is now ready to use and includes:
You can apply the same approach to many other forms that involve a period of time, such as equipment hire, pitch bookings or course registrations.
For more examples, see the articles Using JavaScript to validate a newsletter subscription, Calculated value and JavaScript: Creating a wall colour calculator and Creating an order form with a basket.