•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 402,401 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,915 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2598 | Replies: 1
![]() |
•
•
Join Date: Apr 2007
Posts: 18
Reputation:
Rep Power: 2
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!!
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
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.
<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 9:16 am.
If it only works in Internet Explorer; it doesn't work.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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: tooltip style popup
- Next Thread: Page Cannot Be Displayed - javascript:doNetDetect()


Linear Mode