Skip to content Skip to sidebar Skip to footer

Two Actions In One Form

I have a form and i need to use to actions in it . one for getting informations entered in the fields and redirect the user to another page and the other one for checking the email

Solution 1:

To achieve what you want while keeping it as close to what you had, you can do:

functionvalidateForm() {
  var x = document.forms["myform"]["f1"].value; //changed to "myform" and "f1"var atpos = x.indexOf("@");
  var dotpos = x.lastIndexOf(".");
  if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
      alert("Not a valid e-mail address");
      returnfalse;
  }
}
<!--onsubmit handler added to <form> --><formname="myform"class="login"action="getinfo.php"method="POST"onsubmit="return validateForm()"><inputname="f1"type="text"placeholder="email"autofocus/><inputname="f2"type="password"placeholder="example2"/><inputtype="submit"><!--submit button was missing --></form>

Note that the important part is the submit handler that runs when you submit the form and returns true if you want the form to be submitted or false otherwise, which you were missing.

It's also important to validate on the server side as well, otherwise any user may disable javascript and bypass the validation. As a side note, changing the type of the email input to email already forces validation by the browser, which is easier.

Post a Comment for "Two Actions In One Form"