forked from apu52/Travel_Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment.js
137 lines (114 loc) · 4.64 KB
/
payment.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Function to check if all form fields are filled
function checkFormValidity() {
const form = document.querySelector('form');
const inputs = form.querySelectorAll('input, select');
let isValid = true;
inputs.forEach(input => {
if (!input.checkValidity()) {
isValid = false;
}
});
return isValid;
}
// Add event listener to the form submit event
const form = document.querySelector('form');
const checkoutButton = document.querySelector('.btn-primary');
form.addEventListener('submit', function (event) {
if (!checkFormValidity()) {
event.preventDefault(); // Prevent form submission if fields are not filled
alert('Please fill out all required fields.'); // Display alert message
}
});
const firstNameInput = document.getElementById('firstName');
const lastNameInput = document.getElementById('lastName');
// Regular expression to validate alphabet characters only
const alphabetRegex = /^[A-Za-z]+$/;
firstNameInput.addEventListener('blur', function () {
const value = this.value.trim();
if (!alphabetRegex.test(value)) {
this.setCustomValidity('Please enter a valid first name using only alphabet characters.');
this.classList.add('is-invalid');
} else {
this.setCustomValidity('');
this.classList.remove('is-invalid');
}
});
lastNameInput.addEventListener('blur', function () {
const value = this.value.trim();
if (!alphabetRegex.test(value)) {
this.setCustomValidity('Please enter a valid last name using only alphabet characters.');
this.classList.add('is-invalid');
} else {
this.setCustomValidity('');
this.classList.remove('is-invalid');
}
});
const usernameInput = document.getElementById('username');
// Regular expression to validate username (letters and digits only)
const usernameRegex = /^[a-zA-Z0-9]+$/;
usernameInput.addEventListener('blur', function () {
const value = this.value.trim();
if (!usernameRegex.test(value)) {
this.setCustomValidity('Your username should only contain letters and digits.');
this.classList.add('is-invalid');
} else {
this.setCustomValidity('');
this.classList.remove('is-invalid');
}
});
// Add event listener to the form input event
form.addEventListener('input', function () {
if (checkFormValidity()) {
checkoutButton.removeAttribute('disabled');
} else {
checkoutButton.setAttribute('disabled', 'true');
}
});
// Disable/enable the checkout button based on form validity
form.addEventListener('input', function () {
if (checkFormValidity()) {
checkoutButton.removeAttribute('disabled');
} else {
checkoutButton.setAttribute('disabled', 'true');
}
});
// Add event listener to the Zipcode input for validation
const zipInput = document.getElementById('zip');
zipInput.addEventListener('blur', function () {
const value = this.value.trim();
const isValidZip = /^\d{1,6}$/.test(value); // Check if value contains only digits and is up to 6 digits long
if (!isValidZip) {
this.setCustomValidity('Please enter a valid Zipcode.'); // Set custom validation message
this.classList.add('is-invalid'); // Add Bootstrap invalid class
} else {
this.setCustomValidity(''); // Clear custom validation message if valid
this.classList.remove('is-invalid'); // Remove Bootstrap invalid class
}
});
// Add event listener to the email input for validation
const emailInput = document.getElementById('email');
emailInput.addEventListener('blur', function () {
const email = this.value.trim(); // Trim whitespace from the input
const isValidEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); // Use regular expression to validate email format
if (!isValidEmail) {
this.classList.add('is-invalid'); // Add 'is-invalid' class to input for visual feedback
this.nextElementSibling.style.display = 'block'; // Show the invalid feedback message
} else {
this.classList.remove('is-invalid'); // Remove 'is-invalid' class
this.nextElementSibling.style.display = 'none'; // Hide the invalid feedback message
}
});
const addressInput = document.getElementById('address');
addressInput.addEventListener('blur', validateAddress);
function validateAddress() {
const input = this;
const value = input.value.trim();
const isValid = value !== '';
if (!isValid) {
input.setCustomValidity('Please enter your shipping address.');
input.classList.add('is-invalid');
} else {
input.setCustomValidity('Please enter your shipping address.');
input.classList.remove('is-invalid');
}
}