Skip to content

Commit 4467a27

Browse files
committedJun 18, 2020
Adding ReCAPTCHA to contact form
1 parent 38e7c24 commit 4467a27

File tree

9 files changed

+66
-24
lines changed

9 files changed

+66
-24
lines changed
 

‎_config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ ga_analytics: UA-165403546-1
1717

1818
# Comment settings
1919
comments:
20-
enabled: truec
20+
enabled: true
2121
show_count: true
2222
empty: []
2323
form_action: 'https://agileobjects-postblogcomment.azurewebsites.net/api/PostBlogComment?code=4M3vF5qIkyTCwtOWjvjJzuPWm7DAyKNxhMLLlj2gaFFHSyLUYQRK5A=='
2424
# form_action: 'http://localhost:7071/api/PostBlogComment'
2525

2626
# Contact form settings
2727
contact:
28+
recaptcha_key: '6LctXKYZAAAAAF7Bm_IchT8ZJaqohVK7w-xrPZqy'
2829
form_action: 'https://agileobjects-email.azurewebsites.net/api/SendEmail?code=v/fdE3E7FhUc9QgLaXeOXvOParYBoCXfiFesiy9Y7QEiMOYKq7lVrQ=='
2930
# form_action: 'http://localhost:7071/api/SendEmail'
3031

‎_includes/scripts/comments.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<script src="/assets/js/ao-forms.min.js"></script>
22
<script src="/assets/js/md5.min.js"></script>
3-
<script async src="/assets/js/comments.min.js"></script>
3+
<script src="/assets/js/comments.min.js" async></script>

‎_includes/scripts/contact.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
<script src="/assets/js/ao-forms.min.js"></script>
2-
<script async src="/assets/js/contact.min.js"></script>
1+
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
2+
<script src="/assets/js/ao-forms.min.js"></script>
3+
<script src="/assets/js/contact.min.js" async></script>

‎_sass/_forms.scss

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@
22

33
/* Form fields */
44
input,
5-
textarea {
6-
background: $white;
5+
textarea,
6+
.g-recaptcha {
77
border-radius: 0;
88
box-shadow: none;
99
box-sizing: border-box;
10-
font-size: 16px;
1110
line-height: 1.5;
1211
margin: 10px 0 0 0;
13-
padding: 8px 10px;
1412
vertical-align: baseline;
1513
}
1614

17-
input[type="text"],
15+
input,
1816
textarea {
17+
background: $white;
18+
font-size: 16px;
19+
padding: 8px 10px;
20+
}
21+
22+
input[type="text"],
23+
textarea,
24+
.g-recaptcha {
1925
border: 1px solid $black;
2026
color: $gray-darkest;
27+
}
28+
29+
input[type="text"],
30+
textarea {
2131
display: block;
2232
max-width: 100%;
2333
width: 100%;

‎assets/js/ao-forms.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,20 @@
4545
},
4646
email: function () {
4747
return ao.isEmail(this.e.value);
48+
},
49+
recaptcha: function () {
50+
this.e.value = grecaptcha.getResponse();
51+
return Boolean(this.e.value);
4852
}
4953
};
5054
var validatorNames = Object.keys(validators);
5155
var validatorsCount = validatorNames.length;
5256

53-
var AoValidator = ao.derive(function (input) {
54-
this._base.call(this, input);
57+
var AoValidator = ao.derive(function (inputElemOrId) {
58+
this._base.call(this, inputElemOrId);
5559

5660
this.on('blur keyup', this.validate);
57-
this._msg = ao(input.nextElementSibling);
61+
this._msg = ao(this.e.nextElementSibling);
5862
if (this._msg.hasClass('error-message') === false) {
5963
this._msg = ao('<span>').addClass('error-message');
6064
this.after(this._msg);
@@ -69,16 +73,27 @@
6973
}
7074
});
7175

76+
AoValidator.prototype.setValid = function (valid) {
77+
var remove, add;
78+
if (valid) {
79+
remove = 'error', add = 'valid';
80+
} else {
81+
remove = 'valid', add = 'error';
82+
}
83+
this._msg.removeClass('field-validation-' + remove).addClass('field-validation-' + add);
84+
return this;
85+
};
86+
7287
AoValidator.prototype.validate = function () {
7388
for (var i = 0, l = this._validators.length; i < l; ++i) {
7489
var validator = this._validators[i];
7590
if (validator.test.call(this) === false) {
7691
this._msg.html(validator.msg);
77-
this._msg.removeClass('field-validation-valid').addClass('field-validation-error');
92+
this.setValid(false);
7893
return false;
7994
}
8095
}
81-
this._msg.removeClass('field-validation-error').addClass('field-validation-valid');
96+
this.setValid(true);
8297
return true;
8398
};
8499

@@ -140,6 +155,10 @@
140155
return new AoForm(form);
141156
};
142157

158+
ao.validator = function (inputElemOrId) {
159+
return new AoValidator(inputElemOrId);
160+
};
161+
143162
ao(function () {
144163
var tooltips = ao.getAllByCss('.has-tooltip');
145164
for (var i = 0, l = tooltips.length; i < l; ++i) {

‎assets/js/ao-forms.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎assets/js/contact.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
var ContactForm = function () {
66
function contactForm() {
7-
this.name = ao.get('contact-form-name');
8-
this.email = ao.get('contact-form-email');
9-
this.message = ao.get('contact-form-message');
7+
this._name = ao.get('contact-form-name');
8+
this._email = ao.get('contact-form-email');
9+
this._message = ao.get('contact-form-message');
10+
this._recaptcha = ao.validator('contact-form-recaptcha');
11+
};
12+
13+
contactForm.prototype.recaptchaValid = function () {
14+
this._recaptcha.setValid(true);
1015
};
1116

1217
contactForm.prototype.handleSend = function (form) {
@@ -19,18 +24,19 @@
1924
aoForm.submitting();
2025

2126
var formData = {
22-
name: this.name.value,
23-
email: this.email.value,
24-
message: this.message.value
27+
name: this._name.value,
28+
email: this._email.value,
29+
message: this._message.value,
30+
['g-recaptcha-response']: this._recaptcha.e.value
2531
};
2632

2733
ao.ajax({
2834
type: 'post',
2935
url: form.action,
3036
data: formData,
3137
ctx: aoForm,
32-
onFail: function() { this.error(); },
33-
onSuccess: function() { this.ok(); }
38+
onFail: function () { this.error(); },
39+
onSuccess: function () { this.ok(); }
3440
});
3541

3642
return false;
@@ -49,6 +55,7 @@
4955

5056
ao(function () {
5157
web.contactForm = new ContactForm();
58+
window.recaptchaValid = function () { web.contactForm.recaptchaValid(); };
5259
});
5360
})(ao.Web || (ao.Web = {}));
5461
})(Ao);

‎assets/js/contact.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎contact.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ <h1 class="post-title">Contact</h1>
2626
placeholder="Your message"
2727
data-val="true" data-val-required="A message is required"></textarea>
2828

29+
<div class="g-recaptcha" data-sitekey="{{ site.contact.recaptcha_key }}" data-callback="recaptchaValid"></div>
30+
<input type="hidden" id="contact-form-recaptcha" name="recaptcha"
31+
data-val="true" data-val-recaptcha="Are you a robot...?" />
32+
2933
<input id="contact-form-submit" type="submit" value="Send" />
3034

3135
<div id="progress-cover" class="hidden"></div>

0 commit comments

Comments
 (0)
Please sign in to comment.