jquery - How to prevent form submission from firing continuously? -
i have text field user enters message, clicks enter , submits database. problem is, submits multiple times depend on how many milliseconds enter key pressed. i'm not doing field validation, it's more of proof of concept while learning. know can disable submit form entirely after single submission want able continue entering messages.
<input type="text" placeholder="enter message" id="entermsg"> $(document).ready(function() { $("#entermsg").submit(function(){ var uid = $("#entermsg").val(); messages.insert({text: uid}); })
should using different html tags or jquery functions?
since admit purely acedemic, because no 1 in right mind deploy insecure...
you have global variable determine if submission should accepted or rejected. before submission accepted check global variable. if set true, accept submission , set global variable false. create timeout of, say, 1 second reset global variable true.
this allow 1 submission per second given user.
<form id="myform"> <input type="text" placeholder="enter message" id="entermsg"> <input type="submit" /> </form> var accept_flag = true; time_between_submissions = 1000; $(document).ready(function() { $("#myform").submit(function(){ if (accept_flag) { var uid = $("#entermsg").val(); messages.insert({text: uid}); accept_flag = false; settimeout(function() { accept_flag = true; },time_between_submissions); } });
of course, none of matter because if you're doing proper post form submission, form submitted, browser not going allow resubmitted holding down enter key.
if you're doing via ajax, wouldn't use .submit() method, listen keypress event on form, , yes, you'd need implement way throttle submissions.
Comments
Post a Comment