Skip to content Skip to sidebar Skip to footer

Limit The Number Of Time A Form Can Be Submitted

I have a textarea:
I would like t

Solution 1:

You can store some variables:

$(function () {
   var timesSubmitted = 0;
   var maxSubmits = 5;
   var intervalMilliseconds = 10000; // for testing   
   var interval;   
    $('input[type=submit]').click(function (event) {
        if (!interval) {
            interval = setTimeout(function () {
                interval = undefined;
                timesSubmitted = 0;
                $('div').append('TIMER RESET. Submit again.<br />')
            }, intervalMilliseconds);
        }
        timesSubmitted ++;
        if (timesSubmitted > maxSubmits) {
            $('div').append('NO GO YO!<br />')
        } else {
            $('div').append('valid<br />')   
        }
        event.preventDefault();        
    });        
});​

Example


Solution 2:

I made this quick little sample on jsFiddle. You are probably going to need to warp this in some ajax so you can submit the form without reloading the variables to retain your information.

If you are looking for the AJAX version it would look something

// Setup variable for easy tracking!!!
var timesSubmitted = 0, timeCounter = 60 * 1000, Timer = null;

// Call function to submit form
function submitForm() {
  // Check if user has submitted form more than 5 times
  if (timesSubmitted < 5) {
      // Call ajax to submit form. jQuery Ajax: http://api.jquery.com/jQuery.ajax/
      $.ajax({
          type: "POST",
          url: 'ajaxpage.html',
          data: { test2: $('#test textarea').val() },
          success: function(data) { // if form is submitted successfully
              // If timer has yet to be started... Start it! 
              if (Timer === null) {
                  Timer = setTimeout(
                      function() {  
                          // Setup alert so user knows they can continue to submit form
                          if (timesSubmitted > 0) { alert('Submissions Reset'); }
                          timesSubmitted = 0; 
                      }, 
                  timeCounter );
              }

              // Add for submission to counter
              timesSubmitted++;
            }
      });
  }else{
      // Alert user they can not sumbit anymore!
      alert('You may only submit this from 5 times per minute!');
  }
}

Solution 3:

Like they said, it's not professional, nor secure to do this with javascript. Better server-side, but here you go:

<script type="text/javascript"> 
var flag = false;
function checkTimer(){
    if (flag){
        flag=false;
        return true;
    }
    alert("Don't flood man!");
    return false;
}

//12 seconds, max 5 times per minute
setTimeout(function(){flag=true;},12000);
</script>

And on the form:

<form id='test' onSubmit="return checkTimer()">

Solution 4:

No, there's no way to limit this on client side (you could try to use JavaScript to enforce such limit but it can be easily ommited by disabling JS). You can enforce such limit on your target script and refuse form processing if it comes to often


Post a Comment for "Limit The Number Of Time A Form Can Be Submitted"