-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
84 lines (68 loc) · 2.58 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// ----- Browser-side validation for arrival and departure dates ---//
//Select DOM elements for use in function below
const elArrivalDate = document.getElementById('arrival-date');
const elDepartureDate = document.getElementById('depart-date');
const elDateError = document.getElementById('dateError');
const elForm = document.getElementById('resForm');
var dateIsValid = false;
const getYear = (dateString) => parseInt(dateString.slice(0,4));
const getMonth = (dateString) => parseInt(dateString.slice(5,7));
const getDay = (dateString) => parseInt(dateString.slice(8,10));
//displays an error message if dates are not valid
const rejectDates = () => {
elDateError.className = 'displayError';
dateIsValid = false;
};
//hides error message if dates are valid
const acceptDates = () => {
dateIsValid = true;
elDateError.className = 'hideError';
};
function checkTheDates(arrivalDate, departureDate) {
// Assigning the dates to objects allows us to easily access the year / month / day for arrival and departure dates
const arrival = {
year: getYear(arrivalDate),
month: getMonth(arrivalDate),
day: getDay(arrivalDate)
};
const departure = {
year: getYear(departureDate),
month: getMonth(departureDate),
day: getDay(departureDate)
}
if (arrival.year == departure.year) {
if ( ((arrival.month == departure.month) && (arrival.day < departure.day)) || arrival.month < departure.month ) {
// console.log('Dates OK');
acceptDates();
} else {
// console.log('Dates rejected');
rejectDates();
}
} else if (arrival.year < departure.year) {
// console.log('Dates OK');
acceptDates();
} else {
// console.log('Dates rejected');
rejectDates();
}
}
//Listen for a date change in the Arrival date box
elArrivalDate.addEventListener('change', () => {
console.log(elArrivalDate.value, typeof elArrivalDate.value);
checkTheDates(elArrivalDate.value, elDepartureDate.value);
}, false);
//Listen for a date change in the Departure date box
elDepartureDate.addEventListener('change', () => {
console.log(elDepartureDate.value);
checkTheDates(elArrivalDate.value, elDepartureDate.value);
}, false);
// Listen for form submission
elForm.addEventListener('submit', () => {
//do a final check of the dates
checkTheDates(elArrivalDate.value, elDepartureDate.value);
//prevent submission if dates invalid and alert user
if (dateIsValid != true) {
alert('Invalid dates. Please check the arrival and departure dates and try again.');
event.preventDefault();
}
});