Skip to content

Commit a652c29

Browse files
Merge pull request #315 from Sravan-kumar04/feature/phone
Enhance Contact Form: Phone Validation with Country Code & Future-Date Restriction (Replaces #257)
2 parents e3c62ea + 5343b9c commit a652c29

File tree

4 files changed

+163
-4
lines changed

4 files changed

+163
-4
lines changed

src/contact.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
<title>Contact Us - GrowCraft</title>
88
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
99
<link rel="stylesheet" href="/src/scroll-after-submit.css">
10-
10+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/css/intlTelInput.css" />
11+
<link rel="stylesheet" href="./css/style.css">
12+
<link rel="stylesheet" href="phonevalidator.css" />
1113

1214
<link
1315
rel="stylesheet"
@@ -476,8 +478,8 @@ <h2 class="text-center mb-4 text-bold text-white">Send Us a Message</h2>
476478
<input type="email" class="form-control" required />
477479
</div>
478480
<div class="col-md-6">
479-
<label class="form-label">Phone Number</label>
480-
<input type="text" class="form-control" />
481+
<label class="form-label">Phone Number *</label>
482+
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Enter your phone number" maxlength="15" required/>
481483
</div>
482484
<div class="col-md-6">
483485
<label class="form-label">Company/Organization</label>
@@ -512,7 +514,7 @@ <h2 class="text-center mb-4 text-bold text-white">Send Us a Message</h2>
512514
</div>
513515
<div class="col-md-6">
514516
<label class="form-label">Preferred Date</label>
515-
<input type="date" class="form-control" />
517+
<input type="date" class="form-control" id="PreferredDate" name="PreferredDate" required/>
516518
</div>
517519
<div class="col-md-6">
518520
<label class="form-label">Preferred Time</label>
@@ -692,5 +694,9 @@ <h4>GrowCraft</h4>
692694
});
693695
</script>
694696
<script src="/src/scroll-after-submit.js"></script>
697+
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/js/utils.js"></script>
698+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
699+
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/js/intlTelInput.min.js"></script>
700+
<script src="./phone-validation.js"></script>
695701
</body>
696702
</html>

src/css/style.css

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,3 +1237,67 @@ body.dark-mode .contact-btn:hover {
12371237
background: linear-gradient(135deg, #2563eb, #4f46e5);
12381238
transform: translateY(-2px);
12391239
}
1240+
1241+
1242+
1243+
.navbar.bg-body-tertiary .navbar-nav .nav-link {
1244+
color: #000;
1245+
}
1246+
1247+
/* Dark mode text color */
1248+
[data-bs-theme="dark"] .navbar .nav-link {
1249+
color: #fff;
1250+
}
1251+
1252+
/* Hover color in both modes */
1253+
.navbar .nav-link:hover {
1254+
color: #28a745;
1255+
}
1256+
1257+
1258+
.form-section .iti__country-list {
1259+
background-color: #222 !important;
1260+
color: #fff !important;
1261+
}
1262+
1263+
.form-section .iti__country-list .iti__country {
1264+
color: #fff !important;
1265+
}
1266+
1267+
.form-section .iti__country-list .iti__dial-code {
1268+
color: #ccc !important;
1269+
}
1270+
1271+
.form-section .iti__country-list .iti__highlight {
1272+
background-color: #444 !important;
1273+
}
1274+
1275+
1276+
.form-section label {
1277+
font-weight: 500;
1278+
}
1279+
1280+
.form-section .form-control,
1281+
.form-section .form-select {
1282+
border-radius: 6px;
1283+
}
1284+
1285+
body.dark-mode {
1286+
background-color: #121212;
1287+
color: #ffffff;
1288+
}
1289+
1290+
body.dark-mode a,
1291+
body.dark-mode nav,
1292+
body.dark-mode nav a {
1293+
color: #ffffff;
1294+
}
1295+
1296+
body.light-mode {
1297+
background-color: #ffffff;
1298+
color: #000000;
1299+
}
1300+
1301+
body.light-mode nav a {
1302+
color: #000000;
1303+
}

src/phone-validation.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const phoneInputField = document.querySelector("#phone");
2+
const iti = window.intlTelInput(phoneInputField, {
3+
preferredCountries: ["in", "us", "gb"],
4+
separateDialCode: true,
5+
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/js/utils.js"
6+
});
7+
8+
phoneInputField.addEventListener("input", () => {
9+
phoneInputField.value = phoneInputField.value.replace(/\D/g, "");
10+
});
11+
12+
document.getElementById("contactForm").addEventListener("submit", function (e) {
13+
e.preventDefault();
14+
15+
if (!iti.isValidNumber()) {
16+
phoneInputField.classList.add("is-invalid");
17+
return;
18+
} else {
19+
phoneInputField.classList.remove("is-invalid");
20+
}
21+
const fullPhoneNumber = iti.getNumber();
22+
console.log("Full phone number:", fullPhoneNumber);
23+
24+
this.submit();
25+
});
26+
27+
28+
document.addEventListener("DOMContentLoaded", function () {
29+
const dateInput = document.getElementById("PreferredDate");
30+
const today = new Date().toISOString().split("T")[0];
31+
dateInput.setAttribute("min", today);
32+
});
33+
34+
document
35+
.getElementById("contactForm")
36+
.addEventListener("submit", function (e) {
37+
if (!this.checkValidity()) {
38+
e.preventDefault();
39+
e.stopPropagation();
40+
alert("Please fill all required fields.");
41+
}
42+
});

src/phonevalidator.css

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#phone.is-invalid {
2+
border-color: red;
3+
}
4+
5+
.iti {
6+
width: 100%;
7+
}
8+
9+
10+
#phone {
11+
border: 1px solid #ccc;
12+
padding: 10px;
13+
font-size: 16px;
14+
border-radius: 5px;
15+
width: 100%;
16+
box-sizing: border-box;
17+
}
18+
19+
#phone:focus {
20+
border-color: #007BFF;
21+
outline: none;
22+
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
23+
}
24+
25+
26+
27+
.is-invalid {
28+
border-color: #dc3545 !important;
29+
background-color: rgba(220, 53, 69, 0.1);
30+
}
31+
32+
33+
body.dark-mode .form-label,
34+
body.dark-mode .form-control,
35+
body.dark-mode .form-select {
36+
color: #fff !important;
37+
}
38+
39+
body.dark-mode .form-control {
40+
background-color: #222 !important;
41+
border-color: #444;
42+
}
43+
44+
body.dark-mode .form-select {
45+
background-color: #222 !important;
46+
border-color: #444;
47+
}

0 commit comments

Comments
 (0)