| | |
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
![]() |
•
•
Join Date: Apr 2007
Posts: 18
Reputation:
Solved Threads: 0
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
Thanks in advance!!
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!!
Here is some example code. The returns are the important bit:
Notice this:
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <script> function checkForm() { var oTarget = document.getElementById('must_be_hello'); if(oTarget.value=="hello") { //Returning true! the form will submit. return true; } else { alert("Sorry you cannot submit anything but hello"); //Returning false. Not today thanks. return false; } } </script> </head> <body> <form action="http://google.com" method="POST" onsubmit="return checkForm();"> <input type="text" id="must_be_hello" value="?"/> <input type="submit" value="Submit?"/> </form> </body> </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..
![]() |
Similar Threads
- How would I display somethign only if javascript is enabled? (JavaScript / DHTML / AJAX)
- Javascript - how to stop/halt/break this loop/script plss (JavaScript / DHTML / AJAX)
- Alert box, then cancel action (Javascript) (JavaScript / DHTML / AJAX)
- Does window.stop() work in IE? (JavaScript / DHTML / AJAX)
- Flash Help (Graphics and Multimedia)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: calculate difference of 2 dates
- Next Thread: Page Cannot Be Displayed - javascript:doNetDetect()
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxhelp ajaxjspservlets animate array automatically beta box browser captchaformproblem cart child close codes column css date debugger decimal dependent design disablefirebug dom download element embed engine enter error events ext file firefox focus form forms frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe index java javascript javascripthelp2020 jawascriptruntimeerror jquery jsp jump libcurl listbox maps masterpage math media menu microsoft mimic mp4 onmouseoutdivproblem onmouseover onreadystatechange paypal pdf php player position post problem programming prototype redirect safari scale scriptlets scroll search security select software sql toggle unicode variables w3c web website window windowofwords \n






