0

I want to block the submit of a form in this condition:

1) Without JQuery;

2) With an input type="submit" (instead of type="button"), because it's the only way (i think) to submit form with Enter Key

Here's my code:

<form name="form_ricerca" action="trova_hotels.htm" id="top_form_ricerca" method="post">        
   <fieldset style="margin: 10px;text-align: center;">
     <input id="top_ric_strutt" type="text" name="ricerca" size="40"
     <input onclick="return top_controlla_ricerca();" >
   </fieldset>
</form>

and this is the function:

function top_controlla_ricerca() {
  var nome_codice = document.getElementById("top_ric_strutt").value;
  SOME_CODE
  if (SOME_CONDITIONS) {
   document.getElementById("top_form_ricerca").submit();
   return true;
  } else {
   alert('MY_ALERT');
   return false;
  }
}
Gianluca Demarinis
  • 1,964
  • 2
  • 15
  • 21

1 Answers1

0

1.) add onsubmit listener on the form element instead of the input element

<form onsubmit="return top_controlla_ricerca();" name="form_ricerca" action="trova_hotels.htm" id="top_form_ricerca" method="post">        
   <fieldset style="margin: 10px;text-align: center;">
     <input id="top_ric_strutt" type="text" name="ricerca" size="40"
     <input type="text" />
   </fieldset>
</form>

2.) remove submit manually inside this function ( submit handler )

function top_controlla_ricerca() {
  var nome_codice = document.getElementById("top_ric_strutt").value;
  SOME_CODE
  if (SOME_CONDITIONS) {
   //document.getElementById("top_form_ricerca").submit();  <-- Remove this line
   return true;
  } else {
   alert('MY_ALERT');
   return false;
  }
}
bhavya_w
  • 9,186
  • 9
  • 29
  • 38