HTML button onClick listener calling HTML form onSubmit javascript function

kittu88

I am a novice in web development, I have created a simple html page. The page has two buttons, Submit and Display Data. The Submit button is supposed to post form data to a particular page after validating the form. This button is working fine. I am facing a problem with the Display Data button. The button is supposed to open a separate page and there should not be any kind of form validation. The page is getting open but the form is also getting validated.

The html page:

<html>
<head>
<script>
function validateForm()
{
var name=document.forms["myForm"]["name"].value;
var email=document.forms["myForm"]["email"].value;
var mobile=document.forms["myForm"]["mobile"].value;
var address=document.forms["myForm"]["address"].value;
var atpos=email.indexOf("@");
var dotpos=email.lastIndexOf(".");
if (name==null || name=="")
  {
  alert("Name must be filled out");
  return false;
  }

else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }

  else if(isNaN(mobile)||mobile.indexOf(" ")!=-1)
  {
  alert("Enter numeric value")
  return false; 
  }
  else if (mobile.length != 10)
  {
  alert("Enter 10 digit mobile");
  return false;
  }
  else if (mobile.charAt(0)=="0")
  {
  alert("Mobile no should not start with 0");
  return false;
  }
  else if (address==null || address=="")
  {
  alert("Address must be filled out");
  return false;
  }

}
</script>

</head>

<body>
<h2>Employee Details Entry</h2>
<form name="myForm" action="insertDisplay.php" onSubmit="return validateForm()" method="post">
Name: <input type="text" name="name"><br/>
Email: <input type="text" name="email"><br/>
Mobile: <input type="text" name="mobile"><br/>
Address: <input type="text" name="address"><br/>
<input type="submit" value="Submit"> <button onClick="location.href = 'insertDisplay.php'">Display Data</button>
</form>

</body>

</html>

Where am I going wrong? Why is the form validation function getting called?

user1844933

place <button onClick="location.href = 'insertDisplay.php'">Display Data</button> this line out of the form...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript + HTML: Button Not Calling on Function Onclick

From Dev

HTML form is not calling JS function onsubmit

From Dev

HTML onsubmit event is not calling the JavaScript function

From Dev

Execute onsubmit function in HTML form only with submit button

From Dev

HTML Form OnSubmit function not defined

From Dev

Simple form onsubmit not calling javascript function (JQuery)

From Dev

Javascript create button and trigger onclick function on HTML

From Dev

Simple HTML Javascript button onclick function not working

From Dev

Calling a javascript function on button onclick

From Dev

button and onclick - Html, javascript

From Dev

HTML onClick attribute not calling function

From Dev

basic onclick button in a html form

From Dev

Issue with HTML form submit button not calling Google script function

From Dev

Calling a JavaScript function in HTML?

From Dev

html onsubmit doesn't start Javascript function

From Dev

Calling a function from a HTML button

From Dev

html ,javascript onclick event listener Dom

From Javascript

Calling Javascript from a html form

From Dev

Sending php array to a javascript function with button onclick in html

From Dev

HTML-form submit button not running the OnSubmit script associated with it

From Dev

Calling server function onclick and returning templated html

From Dev

html form does not get sent after onSubmit function

From Dev

Calling a function in HTML vs in Javascript

From Dev

Will adding onclick in HTML and calling from Javascript conflict?

From Dev

javascript function and html onclick property

From Dev

html / javascript onclick function with time

From Dev

Javascript/HTML - repeating a function with onclick

From Dev

HTML form: action vs onsubmit

From Javascript

HTML form action and onsubmit issues

Related Related

  1. 1

    Javascript + HTML: Button Not Calling on Function Onclick

  2. 2

    HTML form is not calling JS function onsubmit

  3. 3

    HTML onsubmit event is not calling the JavaScript function

  4. 4

    Execute onsubmit function in HTML form only with submit button

  5. 5

    HTML Form OnSubmit function not defined

  6. 6

    Simple form onsubmit not calling javascript function (JQuery)

  7. 7

    Javascript create button and trigger onclick function on HTML

  8. 8

    Simple HTML Javascript button onclick function not working

  9. 9

    Calling a javascript function on button onclick

  10. 10

    button and onclick - Html, javascript

  11. 11

    HTML onClick attribute not calling function

  12. 12

    basic onclick button in a html form

  13. 13

    Issue with HTML form submit button not calling Google script function

  14. 14

    Calling a JavaScript function in HTML?

  15. 15

    html onsubmit doesn't start Javascript function

  16. 16

    Calling a function from a HTML button

  17. 17

    html ,javascript onclick event listener Dom

  18. 18

    Calling Javascript from a html form

  19. 19

    Sending php array to a javascript function with button onclick in html

  20. 20

    HTML-form submit button not running the OnSubmit script associated with it

  21. 21

    Calling server function onclick and returning templated html

  22. 22

    html form does not get sent after onSubmit function

  23. 23

    Calling a function in HTML vs in Javascript

  24. 24

    Will adding onclick in HTML and calling from Javascript conflict?

  25. 25

    javascript function and html onclick property

  26. 26

    html / javascript onclick function with time

  27. 27

    Javascript/HTML - repeating a function with onclick

  28. 28

    HTML form: action vs onsubmit

  29. 29

    HTML form action and onsubmit issues

HotTag

Archive