Tepken Vannkorn
Tepken Vannkorn

Reputation: 9723

Javascript timed security to protect spams

I want to detect if spam attacks my contact form. The idea is that I want to test if the user clicks on the submit button after the page loads at least 10 seconds then the form is not sent to the server, otherwise it's eligible. I want to use JavaScript for this. The steps are:

I tried coding like this:

<script type="text/javascript">
   function checkIfSpam( start ) {    // start if the starting time when the page loads
      var end = new Date().getTime() / 1000;  
      var total = end - start;
      if ( total < 10 ) {             // test if it's less than 10 seconds
        alert('You are a spam!');
      } else {
        alert('You are eligible!');
      }
   }
</script>

The problem is that how can I detect the starting time to pass into the function?

Upvotes: 0

Views: 115

Answers (1)

doubledriscoll
doubledriscoll

Reputation: 1179

Above that function, so that it runs when the page is loaded, add this:

var start = new Date().getTime() / 1000;

Then you won't need to pass in the start time at all. Does that work for you?

Upvotes: 3

Related Questions