By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

2 Buttoned Form

TheServant
Expert 100+
P: 1,168
Hi guys,
I am very much a beginner with javascript so count me in with the n00bs here!

I have a form:
[HTML]<form action="training_form.php" method="POST" onsubmit="return check_form()">
<input name="var1" type="text" value="0" />
<input name="var2" type="text" value="0" />
<input name="var3" type="text" value="0" />
<input name="var4" type="text" value="0" />
<input name="recruit" type="submit" value="Recruit" />
<input name="disband" type="submit" value="Disband" />
</form>
[/HTML]

As you can see, it has two submit buttons. What I want to do is validate the data that is about to be submitted, but I need to determine which button is pressed using javascript. Here is my javascript so far:

[HTML]<script type="text/javascript">
function check_form()
{
if ( typeof( window[ 'recruit' ] ) != undefined )
{
if ( document.training_form.var1.value + document.training_form.var2.value + document.training_form.var3.value + document.training_form.var4.value == 0 )
{
alert('You must enter the number of units to train.');
return false;
}
else if ( isNaN(document.training_form.var1.value) || isNaN(document.training_form.var2.value) || isNaN(document.training_form.var3.value) || isNaN(document.training_form.var4.value) || )
{
alert('You may only use numbers.');
return false;
}
else if ( isDigit(document.training_form.var1.value) || isDigit(document.training_form.var2.value) || isDigit(document.training_form.var3.value) || isDigit(document.training_form.var4.value) )
{
alert('You may only use whole numbers.');
return false;
}
else
{
return true;
}
}
}
</script>[/HTML]

At the moment the form still runs regardless of whether recruit or disband is pressed. I think it's because the form is sending a value for recruit when disband is pressed. How can I text if it was recruit or disband that was pressed? I do not want to make many files, but have a simple if statement to distinguish. Also, I have used the isDigit() function. I couldn't get it to work before, so can you tell me if it exists or if I am using it wrong?
Apr 16 '08 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 392
I need to determine which button is pressed using javascript.
The simplest way to do this would be to move the event to the buttons instead of the form. Then have the function being called test the value being passed.

[HTML]<form action="training_form.php" method="POST" >
<input name="var1" type="text" value="0" />
<input name="var2" type="text" value="0" />
<input name="var3" type="text" value="0" />
<input name="var4" type="text" value="0" />
<input name="recruit" type="submit" value="Recruit" onClick="return check_form('Recruit')"/>
<input name="disband" type="submit" value="Disband" onClick="return check_form('Disband')"/>
</form>
[/HTML]




At the moment the form still runs regardless of whether recruit or disband is pressed.
Why wouldn't it? Both buttons are type "submit" so both will submit the form.



I have used the isDigit() function. I couldn't get it to work before
Where you using it in JavaScript. I do not know of any such function in any of the JavaScript DOM references.
Apr 16 '08 #2

TheServant
Expert 100+
P: 1,168
Cheers mate, I will try it when I get home.

Where you using it in JavaScript. I do not know of any such function in any of the JavaScript DOM references.
That would make sense that it doesn't exist. I have been looking for a way to test if something is numeric and not a float. So 123abc, abd, 12.23 will all fail, but 12 or 134 will not. Do you know how I can do this?
Apr 16 '08 #3

TheServant
Expert 100+
P: 1,168
Yeah, I mist be doing the checking wrong. Can you have a look and tell me how to check the variable that I am sending the function?

[HTML]<form action="training_form.php" method="POST">
<input name="var1" type="text" value="0" />
<input name="var2" type="text" value="0" />
<input name="var3" type="text" value="0" />
<input name="var4" type="text" value="0" />
<input name="recruit" type="submit" value="Recruit" onclick="return check_form('recruit')" />
<input name="disband" type="submit" value="Disband" onclick="return check_form('disband')" />
</form>
[/HTML]


Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function check_form ( type_submitted )
  3. {
  4.     if ( type_submitted == 'recruit' )
  5.     {
  6.         if ( document.training_form.var1.value + document.training_form.var2.value == 0 )
  7.         {
  8.             alert('You must enter the number of units to train.');
  9.             return false;
  10.         }
  11.         else if ( isNaN(document.training_form.var1.value) || isNaN(document.training_form.var2.value) )
  12.         {
  13.             alert('You may only use numbers.');
  14.             return false;
  15.         }
  16.         else
  17.         {
  18.         return true;
  19.         }
  20.     }
  21.     if ( type_submitted == 'disband' )
  22.     {
  23.         if ( document.training_form.var3.value + document.training_form.var4.value == 0 )
  24.         {
  25.             alert('You must enter the number of units to train.');
  26.             return false;
  27.         }
  28.         else if ( isNaN(document.training_form.var3.value) || isNaN(document.training_form.var4.value) )
  29.         {
  30.             alert('You may only use numbers.');
  31.             return false;
  32.         }
  33.         else
  34.         {
  35.         return true;
  36.         }
  37.     }
  38. }
  39. </script>
Apr 17 '08 #4

Expert 100+
P: 392
I have been looking for a way to test if something is numeric and not a float. So 123abc, abd, 12.23 will all fail, but 12 or 134 will not. Do you know how I can do this?
To see if a value is numeric you can do this.
[HTML]
<html>
<body></body>
<script>
var numAndLetters = '12drw34';
var num = '1234';
alert(' 1 : is not a number : '+ isNaN(numAndLetters)+'\n 2 : is not a number : '+ isNaN(num));
</script>
</html>

[/HTML]

It test to see if it is a floating point number you could use the indexOf() function to see if it as a "." in it.
Apr 17 '08 #5

Expert 100+
P: 392
Yeah, I mist be doing the checking wrong. Can you have a look and tell me how to check the variable that I am sending the function?
They are correct and working. I added some alert() function calls, and they show the logic is triggering correctly.
Expand|Select|Wrap|Line Numbers
  1. function check_form ( type_submitted )   {
  2.     alert('  function called : '+type_submitted);          
  3.           if ( type_submitted == 'recruit' )  {
  4. alert('Recruit triggered');
  5.               .....
  6.               .....
  7.               .....
  8.  
  9.           if ( type_submitted == 'disband' ) {
  10. alert('Disband triggered');
  11.                             .....
  12.               .....
  13.               .....
  14.  
  15.           }
  16.       }
  17.  
  18.  
  19.  
Apr 17 '08 #6

TheServant
Expert 100+
P: 1,168
They are correct and working. I added some alert() function calls, and they show the logic is triggering correctly.
Expand|Select|Wrap|Line Numbers
  1. function check_form ( type_submitted )   {
  2.     alert('  function called : '+type_submitted);          
  3.           if ( type_submitted == 'recruit' )  {
  4. alert('Recruit triggered');
  5.               .....
  6.               .....
  7.               .....
  8.  
  9.           if ( type_submitted == 'disband' ) {
  10. alert('Disband triggered');
  11.                             .....
  12.               .....
  13.               .....
  14.  
  15.           }
  16.       }
  17.  
  18.  
  19.  
Thanks for your help, I probably had a typo but all your alerts worked and so did mine! Thanks again! I will continue to play around with the float numbers later.
Apr 17 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.