Solved

please see what is wrong with my JS form validation

Posted on 2012-03-20
6
180 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
Comment Utility
You need to return your status:

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

Author Comment

by:jecommera
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:jecommera
Comment Utility
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
Comment Utility
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
Comment Utility
"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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now