-2

Is it possible to add a cancel button in this javascript so the form will not be submitted? This Javascript is currently working in validating these form fields but if for some reason I entered a wrong information there is no way for me to cancel and prevent the form being submitted.

<form METHOD="POST" ACTION="delformaction.cfm" name="deleteform" onSubmit="return validate(deleteform);">

<script language="JavaScript">

function validate(form)
{

    if (form.Institution.selectedIndex == 0)
    {
      alert("You must select an institution for any necessary future correspondence.");
      return false;
    }
    if (form.Requestors_name.value == "")
    {
      alert("You must enter your name for any necessary future correspondence.");
      return false;
    }

    if (form.EntityFirstName.value == "")
    {
      alert("You must enter the Entity's first name.");
      return false;
    }

  alert("Your form has been successfully submitted!");
//  form.reset();
  return true;
}

</script>


  <input type="submit" validate="submitonce" name="DeleteEntity" value="SUBMIT ONCE" class="SubmitButtons">
takendarkk
  • 3,347
  • 8
  • 25
  • 37
user3779216
  • 75
  • 1
  • 8

1 Answers1

0

You can use .preventDefault() to cancel the default action. ie. the form submission.

See this StackOverflow question.

<form METHOD="POST" ACTION="delformaction.cfm" name="deleteform" onSubmit="return validate(event, deleteform);">

<script language="JavaScript">

function validate(e, form)
{
    var fail = false
    if (form.Institution.selectedIndex == 0)
    {
      alert("You must select an institution for any necessary future correspondence.");
      fail = true;
    }
    if (form.Requestors_name.value == "")
    {
      alert("You must enter your name for any necessary future correspondence.");
      fail = true;
    }

    if (form.EntityFirstName.value == "")
    {
      alert("You must enter the Entity's first name.");
      fail = true;
    }

    if (fail) {
        e.preventDefault();
        return false;
    }

  alert("Your form has been successfully submitted!");
//  form.reset();
  return true;
}

</script>


  <input type="submit" validate="submitonce" name="DeleteEntity" value="SUBMIT ONCE" class="SubmitButtons">
Community
  • 1
  • 1
jonofan
  • 371
  • 2
  • 5
  • 14
  • If the question is a duplicate, just flag it as a duplicate instead of duplicating even more content. – takendarkk Mar 03 '17 at 20:30
  • @takendarkk I looked for the duplicate button but didn't find it, thought I didn't have enough rep. But now I see it hidden away under 'flag'. – jonofan Mar 03 '17 at 20:32
  • Thank you but it's not working. when I submitted the form I ddid not get any pop up message. Even the alert box is now not showing – user3779216 Mar 06 '17 at 14:33