Skip to content

Commit d1b3ab6

Browse files
authoredJun 12, 2020
Merge pull request #8 from davidoluseun/jqBValidation
Add Form Validation
2 parents ac3393b + 2a9f4e0 commit d1b3ab6

File tree

5 files changed

+1123
-52
lines changed

5 files changed

+1123
-52
lines changed
 

‎css/style.css

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Form validation */
2+
3+
.help-block ul {
4+
margin: 0;
5+
padding: 0;
6+
list-style: none;
7+
}
8+
9+
.control-group.error .help-block {
10+
color: #dc3545;
11+
}
12+
13+
.control-group.error input,
14+
.control-group.error textarea {
15+
border-color: #dc3545;
16+
}
17+
18+
.control-group.error input:focus,
19+
.control-group.error textarea:focus {
20+
border-color: #dc3545;
21+
-moz-box-shadow: 0 0 0 .2rem rgba(220, 53, 69, .25);
22+
-webkit-box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, .25);
23+
box-shadow: 0 0 0 .2rem rgba(220, 53, 69, .25);
24+
}
25+
26+
.error input:invalid:focus,
27+
.error textarea:invalid:focus {
28+
border-color: #dc3545;
29+
-moz-box-shadow: 0 0 0 .2rem rgba(220, 53, 69, .25);
30+
-webkit-box-shadow: 0 0 0 .2rem rgba(220, 53, 69, .25);
31+
box-shadow: 0 0 0 .2rem rgba(220, 53, 69, .25);
32+
}
33+
34+
.control-group.warning .help-block {
35+
color: #ffc107;
36+
}
37+
38+
.control-group.warning input,
39+
.control-group.warning textarea {
40+
border-color: #ffc107;
41+
}
42+
43+
.control-group.warning input:focus,
44+
.control-group.warning textarea:focus {
45+
border-color: #ffc107;
46+
-moz-box-shadow: 0 0 0 .2rem rgba(222, 170, 12, .5);
47+
-webkit-box-shadow: 0 0 0 .2rem rgba(222, 170, 12, .5);
48+
box-shadow: 0 0 0 .2rem rgba(222, 170, 12, .5);
49+
50+
}
51+
52+
.control-group.success input,
53+
.control-group.success textarea {
54+
border-color: #28a745;
55+
}
56+
57+
.control-group.success input:focus,
58+
.control-group.success textarea:focus {
59+
border-color: #28a745;
60+
-moz-box-shadow: 0 0 0 .2rem rgba(40, 167, 69, .25);
61+
-webkit-box-shadow: 0 0 0 .2rem rgba(40, 167, 69, .25);
62+
box-shadow: 0 0 0 .2rem rgba(40, 167, 69, .25);
63+
}

‎index.html

+23-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<meta content="width=device-width, initial-scale=1" name="viewport">
77
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css">
88
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
9+
<link rel="stylesheet" href="css/style.css">
910
</head>
1011

1112
<body data-spy="scroll" data-target="#site-nav" data-offset="64">
@@ -144,7 +145,7 @@ <h2>Tour Dates</h2>
144145
<div class="card-body">
145146
<img class="card-img img-fluid" src="images/newyork-img.jpg" alt="">
146147
<div class="text-center">
147-
<h3 class="card-title mb-0 pt-3">New York</h3>
148+
<h3 class="card-title h4 mb-0 pt-3">New York</h3>
148149
<p class="card-text">Wednesday 15th January, 2020</p>
149150
<a class="btn btn-dark" href="#">Buy Tickets</a>
150151
</div><!-- .text-center -->
@@ -157,7 +158,7 @@ <h3 class="card-title mb-0 pt-3">New York</h3>
157158
<div class="card-body">
158159
<img class="card-img img-fluid" src="images/paris-img.jpg" alt="">
159160
<div class="text-center">
160-
<h3 class="card-title mb-0 pt-3">Paris</h3>
161+
<h3 class="card-title h4 mb-0 pt-3">Paris</h3>
161162
<p class="card-text">Friday 7th Feburary, 2020</p>
162163
<a class="btn btn-dark" href="#">Buy Tickets</a>
163164
</div><!-- .text-center -->
@@ -170,7 +171,7 @@ <h3 class="card-title mb-0 pt-3">Paris</h3>
170171
<div class="card-body">
171172
<img class="card-img img-fluid" src="images/sanfran-img.jpg" alt="">
172173
<div class="text-center">
173-
<h3 class="card-title mb-0 pt-3">San Francisco</h3>
174+
<h3 class="card-title h4 mb-0 pt-3">San Francisco</h3>
174175
<p class="card-text">Saturday 25th April, 2020</p>
175176
<a class="btn btn-dark" href="#">Buy Tickets</a>
176177
</div><!-- .text-center -->
@@ -189,25 +190,35 @@ <h2 class="text-center">Get in Touch!</h2>
189190
<h3>Fan? Drop a note.</h3>
190191
<p>We like to here from you</p>
191192

192-
<form action="index.html" method="post">
193+
<form class="contact-form" novalidate>
193194
<div class="d-lg-flex">
194-
<div class="form-group col-lg-6 px-0 pr-lg-3">
195+
<div class="control-group form-group col-lg-6 px-0 pr-lg-3">
195196
<label class="form-control-label sr-only" for="v-name">Name</label>
196-
<input class="form-control" type="text" name="v-name" id="v-name" placeholder="Name">
197+
<div class="controls">
198+
<input class="form-control" type="text" name="v-name" id="v-name" placeholder="Name" required>
199+
</div><!-- .controls -->
197200
</div><!-- .form-group -->
198201

199-
<div class="form-group col-lg-6 px-0 pl-lg-3">
202+
<div class="control-group form-group col-lg-6 px-0 pl-lg-3">
200203
<label class="form-control-label sr-only" for="v-email">Email</label>
201-
<input class="form-control" type="email" name="v-email" id="v-email" placeholder="Email">
204+
<div class="controls">
205+
<input class="form-control" type="email" name="v-email" id="v-email" placeholder="Email" required>
206+
</div><!-- .controls -->
202207
</div><!-- .form-group -->
203208
</div><!-- .d-lg-flex -->
204209

205-
<div class="form-group">
210+
<div class="control-group form-group">
206211
<label class="form-control-label sr-only" for="v-comment">Let's have your comment</label>
207-
<textarea class="form-control" name="name" rows="4" cols="50" placeholder="Comment" id="v-comment"></textarea>
212+
<div class="controls">
213+
<textarea class="form-control" name="v-comment" id="v-comment" rows="4" cols="50" placeholder="Comment" required></textarea>
214+
</div><!-- .controls -->
208215
</div><!-- .form-group -->
216+
217+
<div id="feedback"></div><!-- .feedback - success/fail feedback -->
218+
209219
<input class="btn btn-dark" type="submit" value="Submit Comment">
210-
</form>
220+
221+
</form><!-- .contact-form -->
211222
</section><!-- .col-md-7 -->
212223

213224
<aside class="col-md-4 pt-4 pt-md-0">
@@ -353,6 +364,7 @@ <h4>Band</h4>
353364
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
354365
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
355366
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
367+
<script src="js/jqBootstrapValidation.js"></script>
356368
<script src="js/band.js"></script>
357369
</body>
358370
</html>

‎js/band.js

+121-41
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,131 @@
11
(function() {
22
"use strict";
33

4+
// jQuery document ready fn
45
$(function() {
5-
// Use smooth scrolling when clicking on navigation
6-
7-
var topoffset = 54; //variable for menu height
8-
9-
$('.navbar-nav a:not(#search-link, .dropdown-toggle, .dropdown-item)').click(function() {
10-
if (location.pathname.replace(/^\//,'') ===
11-
this.pathname.replace(/^\//,'') &&
12-
location.hostname === this.hostname) {
13-
var target = $(this.hash);
14-
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
15-
if (target.length) {
16-
$('html,body').animate({
17-
scrollTop: target.offset().top-topoffset
18-
}, 500);
19-
return false;
20-
} //target.length
21-
} //click function
22-
}); //smooth scrolling
23-
24-
// Hide and show navigation form
25-
var $searchLink = $("#search-link"); // variable for search link
26-
$searchLink.click(toggleNavForm);
27-
28-
function toggleNavForm(e) {
29-
e.preventDefault();
30-
31-
// Toggle search link aria-expanded attribute value
32-
if ($searchLink.attr("aria-expanded") == "false") {
33-
$searchLink.attr("aria-expanded", "true");
34-
} else {
35-
$searchLink.attr("aria-expanded", "false");
6+
// Use smooth scrolling when clicking on navigation
7+
8+
var topoffset = 54; //variable for menu height
9+
10+
$('.navbar-nav a:not(#search-link, .dropdown-toggle, .dropdown-item)').click(function() {
11+
if (location.pathname.replace(/^\//,'') ===
12+
this.pathname.replace(/^\//,'') &&
13+
location.hostname === this.hostname) {
14+
var target = $(this.hash);
15+
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
16+
if (target.length) {
17+
$('html,body').animate({
18+
scrollTop: target.offset().top-topoffset
19+
}, 500);
20+
return false;
21+
} //target.length
22+
} //click function
23+
}); //smooth scrolling
24+
25+
// Hide and show navigation form
26+
var $searchLink = $("#search-link"); // variable for search link
27+
$searchLink.click(toggleNavForm);
28+
29+
function toggleNavForm(e) {
30+
// Prevent default link behaviour
31+
e.preventDefault();
32+
33+
// Toggle search link aria-expanded attribute value
34+
if ($searchLink.attr("aria-expanded") == "false") {
35+
$searchLink.attr("aria-expanded", "true");
36+
} else {
37+
$searchLink.attr("aria-expanded", "false");
38+
}
39+
40+
// Toggle site search form
41+
$("#nav-search").toggleClass("d-none");
42+
43+
// Toggle site search icon
44+
var $searchIcon = $("#search-icon"); // variable for search icon
45+
$searchIcon.toggleClass("fa-search");
46+
$searchIcon.toggleClass("fa-times");
3647
}
3748

38-
// Toggle site search form
39-
$("#nav-search").toggleClass("d-none");
49+
// Form validation
50+
$(".contact-form input, .contact-form textarea").jqBootstrapValidation({
51+
// Valid inputs success callback fn
52+
submitSuccess: function ($form, event) {
53+
// Prevent default submit behaviour
54+
event.preventDefault();
55+
56+
// Get values from form
57+
var $name = $(".contact-form #v-name").val();
58+
var $email = $(".contact-form #v-email").val();
59+
var $comment = $(".contact-form #v-comment").val();
60+
61+
// For success/failure feedback
62+
var $firstName = $name;
63+
64+
// Check for white space in name for success/fail feedback
65+
if ($firstName.indexOf(" ") >= 0) {
66+
$firstName = $name.split(" ").slice(0, -1).join(" ");
67+
}
68+
69+
// Disable submit button until AJAX call is completed to prevent duplicate messages
70+
var $submitBtn = $(".btn-dark");
71+
$submitBtn.prop("disabled", true).val("Sending...");
72+
73+
$.ajax({
74+
url: "./php/contact.php",
75+
type: "POST",
76+
data: {
77+
name: $name,
78+
email: $email,
79+
comment: $comment
80+
81+
},
82+
cache: false,
83+
success: successFn,
84+
error: errorFn,
85+
complete: completeFn
86+
});
87+
88+
function successFn() {
89+
// Success feedback
90+
$("#feedback").html("<div class='alert alert-success'></div>");
91+
$("#feedback > .alert-success").html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>");
92+
$("#feedback > .alert-success").append($("<strong>").text("Hi " + $firstName + ", Your message has been sent."))
93+
.append('</strong>');
94+
}
95+
96+
function errorFn() {
97+
// Fail feedback
98+
$("#feedback").html("<div class='alert alert-danger'></div>");
99+
$("#feedback > .alert-danger").html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>");
100+
$("#feedback > .alert-danger").append($("<strong>").text("Sorry " + $firstName + ", it seems that my mail server is not responding. Please try again later!"))
101+
.append('</strong>');
102+
}
103+
104+
function completeFn() {
105+
// Clear all fields
106+
$(".contact-form").trigger("reset");
107+
108+
// Re-enable submit button when AJAX call is completed
109+
setTimeout(function() {
110+
$submitBtn.prop("disabled", false).val("Submit Comment");
111+
}, 1500);
112+
113+
// Remove feedback, if focus, click events occur on the page after AJAX call is completed
114+
$("html * ").focus(function() {
115+
$("#feedback").html("");
116+
});
117+
118+
$("html *").click(function() {
119+
$("#feedback").html("");
120+
});
121+
}
122+
123+
}
40124

41-
// Toggle site search icon
42-
var $searchIcon = $("#search-icon"); // variable for search link
43-
$searchIcon.toggleClass("fa-search");
44-
$searchIcon.toggleClass("fa-times");
45-
}
125+
});
46126

47-
// Tooltip activation
48-
$('[data-toggle="tooltip"]').tooltip();
127+
// Tooltip activation
128+
$('[data-toggle="tooltip"]').tooltip();
49129

50-
});
130+
});
51131
})();

‎js/jqBootstrapValidation.js

+912
Large diffs are not rendered by default.

‎php/contact.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
// PHP codes here
3+
4+
?>

0 commit comments

Comments
 (0)
Please sign in to comment.