473,473 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

2 Buttoned Form

TheServant
1,168 Recognized Expert Top Contributor
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
6 1284
pronerd
392 Recognized Expert Contributor
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
1,168 Recognized Expert Top Contributor
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
1,168 Recognized Expert Top Contributor
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
pronerd
392 Recognized Expert Contributor
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
pronerd
392 Recognized Expert Contributor
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
1,168 Recognized Expert Top Contributor
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

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

Similar topics

3
by: John | last post by:
Dear all, It been more than 3 days I am trying to debug this program, I interpret it using activePerl and it is giving (perl -wc code_process.pl) no error syntax but when I put it online, change...
5
by: Richard Cornford | last post by:
I am interested in hearing opinions on the semantic meaning of FORM (elements) in HTML. I have to start of apologising because this question arose in a context that is not applicable to the...
4
by: Targa | last post by:
Trying to total some price fields in a form but doesnt work when all the referenced form fields dont exisit. This is for an invoice - pulled prom a database and the form doesnt always contain the...
25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
19
by: Raposa Velha | last post by:
Hello to all! Does any of you want to comment the approach I implement for instantiating a form? A description and an example follow. Cheers, RV jmclopesAThotmail.com replace the AT with the...
11
by: Jozef | last post by:
I have some old code that I use from the Access 95 Developers handbook. The code works very well, with the exception that it doesn't seem to recognize wide screens, and sizes tab controls so that...
5
by: RAJ | last post by:
hi plz tell me how to know "how window is going to close"... i have to right code for X button of forms... plz telll me thanks bye
6
by: Gary Miller | last post by:
Does anyone know how to detect a modeless form on closing by the form that invoked the modeless form? form.Show();
4
by: Alex Sibilev | last post by:
Hello, I have a really weird problem I've been trying to solve it without any luck for the last couple of hours :( I'm writing a "conference board" application (quite similar to ASP.NET...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.