Suppressing the Enter key from submitting a form

Hello,

We use hand-held barcode scanners attached to our devices to scan certain fields into our Forms - however the barcode scanner naturally sends a carriage return at the end of the barcode scan.

This has the unfortunate side affect of submitting the form, even if the other fields are not yet completed.

I have thought about making all the fields compulsory, as then it would try to submit but this would not happen until you had finished, but not all our fields are compulsory!

Is there any way to disable the carriage return/Enter key in a form/form field from also submitting the Form?

Thanks!

Not sure about that but you can probably configure your barcode reader not to send the carriage return?

Here’s a quick and dirty way to do this:

$(document).on('knack-view-render.form', function(event, view, data) {
  $(`#${view.key} button[type="submit"]`).keydown(function(event){
    if (event.keyCode == 13) {
      event.preventDefault()
      return false
    }
  })
})

Note that this approach only works when the carriage return is focused on the Submit button. You could also prevent the enter key from firing on the entire form but then you lose the ability to enter on paragraph fields or rich text, for example.

A more elegant way would be to validate the form and manually submit it, rather than simply saying return false.

Cheers,
Ian
Knack Pros