-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcars.js
50 lines (41 loc) · 1.58 KB
/
cars.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//importing cities data
// Load cities data from cities.json dynamically
let cityData = { cities: [] }; // Default structure in case fetch fails
fetch("./cities.json")
.then((response) => response.json())
.then((data) => {
cityData = data;
})
.catch((error) => {
console.error("Error loading city data:", error);
});
function validateCarForm() {
const checkInDate = new Date(document.getElementById("carCheckIn").value);
const checkOutDate = new Date(document.getElementById("carCheckOut").value);
const startDate = new Date("2024-09-01");
const endDate = new Date("2024-12-01");
const city = document.getElementById("carCity").value.toLowerCase();
const validCities = cityData.cities;
if (!validCities.includes(city)) {
alert("City must be in Texas or California.");
return;
}
if (checkInDate < startDate || checkOutDate > endDate) {
alert(
"Check-in and Check-out dates must be between Sep 1, 2024, and Dec 1, 2024."
);
return;
}
if (checkInDate >= checkOutDate) {
alert("Check-in date must be before the Check-out date.");
return;
}
const carType = document.getElementById("carType").value;
// Displaying all the information entered by the user in the alert
const message = `City: ${
city.charAt(0).toUpperCase() + city.slice(1)
}\nCar Type: ${carType}\nCheck-in Date: ${checkInDate.toDateString()}\nCheck-out Date: ${checkOutDate.toDateString()}`;
alert(message);
// Reset the form after successful validation
document.getElementById("validateCarForm").reset();
}