Skip to content

Commit 02af838

Browse files
omartanmckennapsean
authored andcommitted
Honeypot Instructions (#106)
* Honeypot Instructions * fixed honeypot * Made Honeypot as optional
1 parent 59838ff commit 02af838

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,34 @@ e.g:
293293

294294
Let us know if you have any questions!
295295

296+
## SPAM prevention
296297

298+
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.
299+
300+
```html
301+
<form action="https://script.google.com/macros/s/..." method="post">
302+
<!--input id must be honeypot or else it wont work-->
303+
<label class="sr-only">Keep this field blank</label>
304+
<input id="honeypot" type="text" name="honeypot" value="" />
305+
<!--the rest of your form-->
306+
</form>
307+
```
308+
309+
```css
310+
#honeypot {
311+
display: none; /*makes the field not visible to humans*/
312+
}
313+
```
314+
315+
```javascript
316+
/* form-submission-handler.js */
317+
/* remove the comment from this if statement */
318+
319+
if (validateHuman(data.honeypot)) { //if form is filled, form will not be submitted
320+
return false;
321+
}
322+
323+
```
297324

298325

299326
## Background Reading

form-submission-handler.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ function validEmail(email) { // see:
33
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
44
return re.test(email);
55
}
6+
7+
function validateHuman(honeypot) {
8+
if (honeypot) { //if hidden form filled up
9+
console.log("Robot Detected!");
10+
return true;
11+
} else {
12+
console.log("Welcome Human!");
13+
}
14+
}
15+
616
// get all data in form and return object
717
function getFormData() {
818
var elements = document.getElementById("gform").elements; // all form elements
@@ -45,6 +55,13 @@ function getFormData() {
4555
function handleFormSubmit(event) { // handles form submit withtout any jquery
4656
event.preventDefault(); // we are submitting via xhr below
4757
var data = getFormData(); // get the values submitted in the form
58+
59+
/* OPTION: Remove this comment to enable SPAM prevention, see README.md
60+
if (validateHuman(data.honeypot)) { //if form is filled, form will not be submitted
61+
return false;
62+
}
63+
*/
64+
4865
if( !validEmail(data.email) ) { // if email is not valid show error
4966
document.getElementById('email-invalid').style.display = 'block';
5067
return false;

0 commit comments

Comments
 (0)