Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

please see what is wrong with my JS form validation

Posted on 2012-03-20
6
183 Views
Last Modified: 2012-03-20
Hi

I am testing some JS and the following code seems to still submit the form even when the confirm if cancelled returns false.

Can you please see why this is happening:

thanks

<html>
<head><title>Id and Name Attributes</title>

<script type="text/javascript">

function readySubmit() {

      if (confirm("Are you ready to submit your form?")) {
            return true;
      }
      else {
            alert("boo");
            return false;
      }
}

</script>

</head>
<body>
<form action="submit.php" method="get">
Enter your user id:
<input type="text" name="textbox" value="" />
<br />
Type your password:
<input type = "password" name = "secret" />
<p></p>
<input type="submit" onClick="readySubmit();" />

</form>


</body>
</html>
0
Comment
Question by:jecommera
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:onlyaymie
ID: 37744456
You need to return your status:

<input type="submit" onClick="return readySubmit();" />
0
 

Author Comment

by:jecommera
ID: 37744464
I thought you would return from a function rather than a method?

sorry just trying to get my head around this...
0
 
LVL 5

Expert Comment

by:onlyaymie
ID: 37744492
I'm not clear on what you are asking, but you have to return false from an onclick to stop the submit from firing.

Does that help?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:jecommera
ID: 37744499
We have return true and return false which I understand says ye or nay to submit

I don't really understand the third return "return readySubmit();"
0
 

Author Comment

by:jecommera
ID: 37744503
where is it returning to - the function? Isn't it calling rather than returning?
0
 
LVL 5

Accepted Solution

by:
onlyaymie earned 500 total points
ID: 37744535
"return readySubmit();"  just returns what your JavaScript method returns.  Without that return in the onClick, your method's return isn't used for anything.

You could also simplify it to just:

<html>
<head><title>Id and Name Attributes</title>

</head>
<body>
<form action="submit.php" method="get">
Enter your user id:
<input type="text" name="textbox" value="" />
<br />
Type your password:
<input type = "password" name = "secret" />
<p></p>
<input type="submit" onClick="return confirm('Are you ready to submit your form?');" />

</form>


</body>
</html>

Open in new window


But I'm assuming you plan to do more in your JavaScript once you get it working.  The key is, to stop the input's action, you have to return false from your onclick; otherwise the default behavior (submitting) continues.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question