简体   繁体   中英

Form validation function doesn't run on submit

I have a function that I'm using to validate my form values. I've added it to the onsubmit attribute, but it doesn't run when it should. I've put it inside the body and before the submit button, but the page runs the function at the very beginning.

I want for checkForm to run at the end to validate the entry of data, but whenever I submit the form, the page just refreshes.

Is my function defined or called in the wrong place? What changes do I need to make to stop the page from refreshing and have my function validate the form data?

<div id="mc_embed_signup">
  <form id="form" method="post" onsubmit="return checkForm(this)">

  <script>
    function checkForm(form)
    if(radioValue = checkRadio(form.radiofield)) {
      return true;
    else {
      alert("Seleccione un tipo de operación");
      return false;
    }
    return true;        
  </script>

You can use preventDefault() of JS to prevent refresh when submiting

For more detail -> go here : https://www.w3schools.com/jsref/event_preventdefault.asp

<script>
     $("#form").on('submit', function(e){
           e.preventDefault();
           // call your function
          $("#form").submit ();
      });
</script>

this should work;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM