User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2007
Posts: 18
Reputation: Arch_Bytes is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Arch_Bytes Arch_Bytes is offline Offline
Newbie Poster

Help JavaScript to stop the action

  #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!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 47
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: JavaScript to stop the action

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

<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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 1:34 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC