Skip to content

Commit e5e01a8

Browse files
authored
Merge pull request #328 from dwyl/default-to-honeypot
Fixes #321 by adding honeypot spam prevention
2 parents 1ea724e + 798d9c5 commit e5e01a8

File tree

4 files changed

+22
-36
lines changed

4 files changed

+22
-36
lines changed

README.md

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -340,35 +340,6 @@ e.g:
340340

341341
Let us know if you have any questions!
342342

343-
## SPAM prevention
344-
345-
In order to avoid getting spammed and fill up google apps usage quota, we will be implementing a simple SPAM prevention technique that's known as Honeypot where it essentially creates a hidden text field that if filled up is assumed as a spam bot and prevents the form from submit.
346-
347-
```html
348-
<form action="https://script.google.com/macros/s/..." method="post">
349-
<!--input id must be honeypot or else it wont work-->
350-
<label class="sr-only">Keep this field blank</label>
351-
<input id="honeypot" type="text" name="honeypot" value="" />
352-
<!--the rest of your form-->
353-
</form>
354-
```
355-
356-
```css
357-
#honeypot {
358-
display: none; /*makes the field not visible to humans*/
359-
}
360-
```
361-
362-
```javascript
363-
/* form-submission-handler.js */
364-
/* remove the comment from this if statement */
365-
366-
if (validateHuman(data.honeypot)) { //if form is filled, form will not be submitted
367-
return false;
368-
}
369-
370-
```
371-
372343

373344
## Uploading Files
374345

form-submission-handler.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(function() {
2+
23
function validateHuman(honeypot) {
34
if (honeypot) { //if hidden form filled up
45
console.log("Robot Detected!");
@@ -11,9 +12,14 @@
1112
// get all data in form and return object
1213
function getFormData(form) {
1314
var elements = form.elements;
15+
var honeypot;
1416

1517
var fields = Object.keys(elements).filter(function(k) {
16-
return (elements[k].name !== "honeypot");
18+
if (elements[k].name === "honeypot") {
19+
honeypot = elements[k].value;
20+
return false;
21+
}
22+
return true;
1723
}).map(function(k) {
1824
if(elements[k].name !== undefined) {
1925
return elements[k].name;
@@ -48,22 +54,23 @@
4854
// add form-specific values into the data
4955
formData.formDataNameOrder = JSON.stringify(fields);
5056
formData.formGoogleSheetName = form.dataset.sheet || "responses"; // default sheet name
51-
formData.formGoogleSendEmail = form.dataset.email || ""; // no email by default
57+
formData.formGoogleSend
58+
= form.dataset.email || ""; // no email by default
5259

5360
console.log(formData);
54-
return formData;
61+
return {data: formData, honeypot};
5562
}
5663

5764
function handleFormSubmit(event) { // handles form submit without any jquery
5865
event.preventDefault(); // we are submitting via xhr below
5966
var form = event.target;
60-
var data = getFormData(form); // get the values submitted in the form
67+
var formData = getFormData(form);
68+
var data = formData.data;
6169

62-
/* OPTION: Remove this comment to enable SPAM prevention, see README.md
63-
if (validateHuman(data.honeypot)) { //if form is filled, form will not be submitted
70+
// If a honeypot field is filled, assume it was done so by a spam bot.
71+
if (formData.honeypot) {
6472
return false;
6573
}
66-
*/
6774

6875
disableAllButtons(form);
6976
var url = form.action;

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ <h2 class="content-head is-center">Contact Us!</h2>
5050
<input id="color" name="color" placeholder="green" />
5151
</fieldset>
5252

53+
<fieldset class="pure-group honeypot-field">
54+
<label for="honeypot">To help avoid spam, utilize a Honeypot technique with a hidden text field; must be empty to submit the form! Otherwise, we assume the user is a spam bot.</label>
55+
<input id="honeypot" type="text" name="honeypot" value="" />
56+
</fieldset>
57+
5358
<button class="button-success pure-button button-xlarge">
5459
<i class="fa fa-paper-plane"></i>&nbsp;Send</button>
5560
</div>

style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ button {
3535
#name, #email {
3636
width: 50%;
3737
}
38+
.honeypot-field {
39+
display: none;
40+
}

0 commit comments

Comments
 (0)