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();
});
});
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 formfunctionsubmitForm() {
// Check if user has submitted form more than 5 timesif (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 formif (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:
<scripttype="text/javascript">var flag = false;
functioncheckTimer(){
if (flag){
flag=false;
returntrue;
}
alert("Don't flood man!");
returnfalse;
}
//12 seconds, max 5 times per minutesetTimeout(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"