How to prevent from bots. Alternative for recaptcha

Prevent from bots

Hello, today I am gonna show you simply trick which you can use to prevent bots from filling forms on your website.

Of course the best solution to prevent your website from bots is recaptcha, so if you only can use it, do it. But are there some situations when you can’t or do not want to use captcha, then it is worthy to know this workaround (this one od solutions suggested by Salesforce in documention – link in Resources).

NOTE: I will show you this solution on Salesforce example but can be used in any environment.

We implement two steps bots protection:

  1. Hidden field – bots can’t read css, so we create one additional fiel in our form, which will be hidden to user with css, but visible for bots. Then, before form submission we just need to check if this field is still empty.
  2. Javascript timer- we also implement functionality which check, how long it took to user to fill form. Depends on form complexity we can estimate what is minimum time for that and reject all attempts which were shorter than that.

NOTE: For this tutorial I am using SelfRegister component for Salesforce Community, click here to learn more about it.

Code

So, let’s modify your Lightning component, add below lines of code to Lightning bundle

z_LightningSelfRegisterControllerCustom.cmp

<aura:attribute name="checkField" type="String"/>
<aura:attribute name="timeStamp" type="Integer"/>
<aura:handler name="init" value="{!this}" action="{!c.initialize}"/>

<lightning:input
 name="checkField"
 label="checkField"
 class="testFieldClass"
 type="text"
 value="{!v.checkField}"/>

  • 1 line: This attribute keeps value of test field
  • 2 line: This attribute keeps time of component render
  • 3 line: Handler for init event
  • 5 line: Test field, it will be hidden for user

z_LightningSelfRegisterControllerCustom.css

.THIS .testFieldClass {
   display: none !important;
}
  • 2 line: We ensure that test field is hidden to user.

z_LightningSelfRegisterControllerCustomHelper.js

setTimeStamp: function(component) {
        component.set("v.timeStamp", Math.floor(Date.now() / 1000))
    }
  • 2 line: Set current time in seconds

NOTE: below code must be in place where is submit your form (before it)

if(!$A.util.isEmpty(component.get("v.checkField")) || ( Math.floor(Date.now() / 1000) - component.get("v.timeStamp") < 5)) {
     //reject request here....
     component.find('userValidationMessage').setError('Please take your time while filling the form');
} else {
     //approve action and submit form here
     $A.enqueueAction(submitForm);
}
  • 1 line: In this line we check form under two conditions. if test field is empty and if form has been filled in more then 5 seconds (in my case, you need to estimate the minimal duration od filling)
  • 3 line: We reject submit request if it doesn’t pass our test
  • 5 line: We submit data (in my case, call action from controller)

z_LightningSelfRegisterControllerCustomController.js

initialize: function(component, event, helper) { 
    helper.setTimeStamp(component);
}
  • 2 line: Set current time

Summary

And that’s all you need to know to protect from bots your website, if you have any question or feedback, leave comment.

See you soon!

Resources:

5 1 vote
Article Rating
Subscribe
Notify of
guest
10 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
deviljhon798
deviljhon798
1 year ago

This is a really off topic, Howdy, but I wanted to know if WYSIWYG editors are used by blogs,Or if you immediately have to code with HTML. I ‘m starting to A blog soon, but I have no experience with coding, so I needed advice.By someone who’s got experience. It would be necessary for some help,Enormously respected! One of the finest Functional Testing facilities for you is ASTAQC (https:/astaqc.com / functional-testing/). This will help to ensure that the program or system ‘s functionality is as planned.

jhondevid62
jhondevid62
1 year ago

Nice idea it also allows the tester to do smarter research in less time thanks to sharing this useful knowledge with us.

trackback
1 year ago

Google

One of our guests a short while ago recommended the following website.

Daniel Davies
1 year ago

Thanks for sharing informative tips for preventing against spambots. But according to me Captcha is the best solution because without a physical barrier like captcha it is impossible to stop bot.

NYGCI HR
1 year ago

Hi there,I enjoy reading through your article post, I wanted to write a little comment to support you and wish you a good continuationAll the best for all your blogging efforts.

Salesforce Consulting services in India

krishna
1 year ago

Hi,
As I have to go through your blog I found it very interesting because you describe how to simply prevent from bots. But I think you forget to describe its process.I also write similar blogs if you want to add more to your blog then do visit http://www.cloudanalogy.com

Close Menu
10
0
Would love your thoughts, please comment.x
()
x