JavaScript to stop the action

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Apr 2007
Posts: 18
Reputation: Arch_Bytes is an unknown quantity at this point 
Solved Threads: 0
Arch_Bytes Arch_Bytes is offline Offline
Newbie Poster

JavaScript to stop the action

 
0
  #1
May 29th, 2007
In my Submit button I have a JavaScript coded that will validate certain textfields.. The problem is if the value in the text field fails I must make away to stop the form from submitting..

any suggestions? I'm using JSF with this Javascript.. and to get a picture of my button here's the code for that

<h:commandButton value="Submit" 
action="#{statisticsController.someProccess}"
onclick="javascript:showItem(10,document.getElementById('statForm:txtTo').value,document.getElementById('statForm:txtFrom').value);" />

Thanks in advance!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: JavaScript to stop the action

 
1
  #2
May 29th, 2007
Here is some example code. The returns are the important bit:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script>
  4. function checkForm()
  5. {
  6. var oTarget = document.getElementById('must_be_hello');
  7. if(oTarget.value=="hello")
  8. {
  9. //Returning true! the form will submit.
  10. return true;
  11. }
  12. else
  13. {
  14. alert("Sorry you cannot submit anything but hello");
  15. //Returning false. Not today thanks.
  16. return false;
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <form action="http://google.com" method="POST" onsubmit="return checkForm();">
  23. <input type="text" id="must_be_hello" value="?"/>
  24. <input type="submit" value="Submit?"/>
  25. </form>
  26. </body>
  27. </html>

Notice this:
<form action="http://google.com" method="POST" onsubmit="return checkForm();">

Returning this function to the form onsubmit event will make the submit dependant on the return of the function. So, if the function returns false; the onsubmit is cancelled; if the function returns true; the onsubmit continues.

The checkForm function is a silly example; but remember the function(s) that you do use should return a 'true' if submission should continue; and a 'false' if it should be aborted; and importantly; remember that onsubmit is a form event, not a submit button event.
Last edited by MattEvans; May 29th, 2007 at 10:16 am.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC