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?