Forums

Locking down Enter key

itpates 25 Oct, 2018
I have a form used for entering retail close-out counts. I have already introduced code to limit the fields to just numbers, periods, and allow for back-space and tab. But some have complained that they accidentally hit the enter key when filling out the form, which submits it prematurely. I tried to lock out the enter key too, but it either killed the script altogether, allowing any key to type in the fields, or just flat out didn't work. Here is my working code. I tried adding an && event.which == 13, and even tried to put in another if statement with that but no avail. Ideas?
$(".allownumericwithdecimal").on("keypress keyup blur",function (event) {

if((event.which >= 48 && event.which <= 57) && event.shiftKey) {
event.preventDefault();
}

if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57) && event.which != 8 && event.which != 45 && event.keyCode != 9 ) {
event.preventDefault();
}

});
healyhatman 26 Oct, 2018
Use jQuery instead of $

And you haven't got the event.which == 13 in there, care to show us your code as it is now?
itpates 26 Oct, 2018
$( ); has been working perfectly with this code and other code I have in JQuery.

This is the working version that locks out non-numeric characters. Am not sure how I can modify this to include blocking the Enter key.

Below are two things I tried. The first version breaks the script altogether, allowing letters to be typed in fields and not blocking Enter key, and the second version simply...doesn't work. At all.
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57) && event.which != 8 && event.which != 45 && event.keyCode != 9 && event.which == 13) {
event.preventDefault();
}

// or...

if (event.which == 13) { event.preventDefault(); }
healyhatman 26 Oct, 2018
Well that won't work you've told it

If (all of these things) AND it equals 13. Should be || event.which ==13 surely?
itpates 26 Oct, 2018
DOH. Got hung up on all the &&'s. However, while || event.which == 13 does not break the code, it still does not work. On a lark, I tried || event.which = 13 and || event.which == '13' with the same non-result. Is there another way to express the Enter key?
healyhatman 26 Oct, 2018
I don't know why it wouldn't be working, try with JUST the == 13 and if it doesn't work it's probably bound wrong
itpates 26 Oct, 2018
Tried it with this and it still does not work. If it is bound wrong, how can it block all the other keys I want blocked, and totally ignore anything having to do with the Enter key?
$(".allownumericwithdecimal").on("keypress keydown keyup blur", function (event) {

if ( event.which == 13 || event.which == 10 ) {
event.preventDefault();
}

});
itpates 26 Oct, 2018
I even changed how the function was bound to the document. But, everything except for the Enter key blocking works perfectly. Something is implicitly BLOCKING blocking the enter key.
jQuery(document).on('keypress', 'input[type="text"]', function(event) {
if((event.which >= 48 && event.which <= 57) && event.shiftKey) {
event.preventDefault();
}

if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57) && event.which != 8 && event.which != 45 && event.keyCode != 9 ) {
event.preventDefault();
}

if (event.which == 13 || event.which == 10 ) {
event.preventDefault();
}

});
itpates 26 Oct, 2018
Does anyone have a simple confirmation code for the submit button? I even tried adding onclick:return confirm('Are you sure you wish to submit this?'); to the "Extra attributes" field for the Submit button, which displayed in the code correctly, but had absolutely no affect whatsoever.
itpates 26 Oct, 2018
No errors whatsoever.
itpates 26 Oct, 2018
I ended up using this - which stops the submission with the warning, but when Cancel is clicked, it does not return to a live form. You just see a faded version of the form and input info with no way to edit it. Refreshing that presents the user with a blank form, forcing them to retype everything again.

jQuery('#display-section1').submit(function() { return confirm("Click OK to continue?"); });
This topic is locked and no more replies can be posted.