Solved

please see what is wrong with my JS form validation

Posted on 2012-03-20
6
181 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending E-mail ASP.Net 3 55
rhino JavaScript import, load 25 66
Javascript Hashmap output 8 44
Why does my button disappear? 3 26
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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 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…

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now