I have a weird need here. I'm using a bar code scanner to get serial numbers and such from large inventory and then follow up shipping tags. This will be entered into a CCK type field. No biggie. The problem is I've tried three different bar code scanners and all return to the computer a 'return' '\n' 'enter' however you want to describe it after reading the bar code. This unfortunately cause the web form to submit, even if other fields have not been completed. Is there a way to disable the enter key within the browser through Javascript or something -- ie: so you have to use a mouse to press the submit button?

Comments

nedjo’s picture

You could attach a handler to the form's submit event and proceed with the submit only if the event target is the submit button. Something like:

$('#myform-id').submit(function(event) {
  var $target = $(event.target);
  if ($target.is('input[type=submit]')) {
    return true;
  }
  return false;
});
Havamal’s picture

Thanks, nedjo, for the help. I found a similar script to do this on the web, but I wasn't sure how to insert in the 'Drupal' environment. If I created the form in CCK, do I then need to create a module that somehow applies the above code into my specific node type? Or is this something I just enter into the form creating the content type?

Havamal’s picture

I may just try this out myself and report back, but I'm guessing the instructions in the following link might do the trick, related to 6.x which I'm using. Specifically, copy code to custom .js file and then alter theme file to load my .js.

http://drupal.org/node/336122

blogook’s picture

I am trying to get this done .. but without success. I need to prevent that by pressing enter it will submit the form. I read somewhere that when using AJAX in your form it works slightly differently, I tried the code above in two different example modules. One with AJAX and one without, both of them still accept 'enter' as submitting.

does your code actually work, I ask this because you type 'Something like:'

blogook’s picture

found a solution:

$form['#attributes'] = array('onsubmit' => 'return false');

blocks submit all together, alternatively:

  $('#edit-searchfield').keypress(function(event) {
   if (event.keyCode == '13') {
       event.preventDefault();
   }
});