Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 validation . the email validation is for the first fields ,the other field is normal here's my code :

<form name="myform" class="login" action="getinfo.php" method="POST">
<input name="f1" type="text" placeholder="email" autofocus/>
<input name="f2" type="password" placeholder="example2"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
287 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

function validateForm() {
  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");
      return false;
  }
}
<!--onsubmit handler added to <form> -->
<form name="myform" class="login" action="getinfo.php" method="POST" onsubmit="return validateForm()"> 
  <input name="f1" type="text" placeholder="email" autofocus/>
  <input name="f2" type="password" placeholder="example2"/>
  <input type="submit"> <!--submit button was missing -->
</form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...