Creating a booking form for a holiday let

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:

  1. €95.00 per night
  2. a one-off cleaning fee of €60.00 per stay, regardless of how many nights guests stay
  3. minimum stay: 3 nights

At the end of the article, you'll also find out how to set different prices for peak and off-peak season.

Why dates can be tricky

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:

  1. A date starts out as text. Formcentric stores all form values as strings. To JavaScript, 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.
  2. Text can't be compared like dates. Compared as text, 09/08/2027 comes before 10/07/2027, because 0 comes before 1. Comparisons only work reliably with real date values.
  3. Months start at 0. In JavaScript, January is month 0 and December is month 11. If you forget this, you'll end up a month out.
  4. Not every day has 24 hours. When the clocks change, one day in March has 23 hours and one day in October has 25. Without rounding, your calculation might return 4.96 nights instead of 5.

Building the form

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.

Page 1: Travel dates

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.

Page 2: Contact details

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.

Validating the dates

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.

Setting up validation

  1. Open page 1 and go to the Validation section in the editing area.
  2. Slide the Validation toggle to the right to enable it.
  3. Select the JavaScript option.
  4. Paste the following code into the Validation script field.

What the code does

  1. toDate() converts the text entered into a real date. If the text isn't a valid date, the function returns null.
  2. The checks run one after the other. As soon as a condition isn't met, the function returns the relevant error message and stops.
  3. (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.
  4. If the function returns an empty string, all checks have been passed.

Calculating the nights and price

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:

  1. Show value in form
  2. Recalculate value directly in browser

Calculated value for the number of nights

Setting Value
Label Number of nights
Technical name nights
JavaScript See the code block below

What the function does

  1. It converts the arrival and departure dates into date values and works out the difference in nights.
  2. As long as a date is missing or the departure date isn't after the arrival date, nothing is displayed.
  3. The result is shown with its unit – for example, 5 nights.

Calculated value for the total price

Setting Value
Label Total price
Technical name totalprice
JavaScript See the code block below

What the function does

  1. It reads the nights calculated value. parseInt() only takes the number at the start, so 5 nights becomes 5.
  2. It multiplies the number of nights by the price per night and adds the cleaning fee once.
  3. As in the article on order forms, it calculates in cents and only converts the result into euros for display.

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).

Going further: seasonal pricing

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

  1. It starts with the cleaning fee as the base amount.
  2. The loop moves from one night to the next. For each night, it checks the month and adds the appropriate price.
  3. 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.

Troubleshooting

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

Testing

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.

Summary

Your booking form is now ready to use and includes:

  1. a check that the arrival and departure dates are valid and make sense together
  2. a minimum stay
  3. automatic calculation of the number of nights
  4. a total price including the cleaning fee, with optional seasonal pricing

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.

Feedback