?
Solved

please see what is wrong with my JS form validation

Posted on 2012-03-20
6
Medium Priority
?
199 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

807 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