Solved

please see what is wrong with my JS form validation

Posted on 2012-03-20
6
182 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

770 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