block the enter within an input

3

I have the following input inside a form:

<input type='text' class='form_campos form_campos_nome' id='auto' name='verifica_nome3'>

I need to block the enter key inside it, ie when the cursor is inside it enter does not send the form, but if it is inside any other input enter can send the form.

Does anyone know how to do this?

    
asked by anonymous 28.06.2016 / 18:23

3 answers

1

Use the event for all inputs but delete esse:

$('input').not('#auto').keypress(function(e) {
    if(e.which == 13) { // se pressionar enter
        console.log('pode submeter'); // aqui pode submeter o form
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype='text'class='form_camposform_campos_nome'id='auto'name='verifica_nome3'><inputtype="text" placeholder="Se o cursor estiver aqui pode submeter form">

EXAMPLE in jsfiddle

Or the other way around (probably this fits more to what you want):

$('#auto').keypress(function(e) {
    if(e.which == 13) {
      e.preventDefault();
      console.log('Não vou enviar');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form><inputtype='text'class='form_camposform_campos_nome'id='auto'name='verifica_nome3'><inputtype="text" placeholder="Se o cursor estiver aqui pode submeter form">
<input type="submit">
</form>

EXAMPLE in jsfiddle

    
28.06.2016 / 18:32
3

You can add a function to your input that does nothing when the enter is pressed.

<input type='text' class='form_campos form_campos_nome' id='auto' name='verifica_nome3' onkeypress="doNothing()">

function doNothing() {  
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if( keyCode == 13 ) {


  if(!e) var e = window.event;

  e.cancelBubble = true;
  e.returnValue = false;

  if (e.stopPropagation) {
    e.stopPropagation();
    e.preventDefault();
  }
} 
    
28.06.2016 / 18:30
1

If it's just to lock enter, just do this:

$(function() {
   $('form').submit(function(event){
       return checkFocus();
    });
 });

 function checkFocus() {
  if ($('#auto').is(':focus')) {
     return false;
  }
  return true;
 }

Here the working example

    
28.06.2016 / 18:49