Which Submit button was pressed?

S

Syed Ali

Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?

I used -onSubmit with the HTML form tab, but I am not sure how to
check which button was pressed, i.e., how do I reference submit1,
submit2 button in my logic of:
if submit1 then
if all textfields have been completed then
return true
else
return false

I also tried to used -onClick with each Submit button, but returning
false if all textfields have not been filled still causes the form to
get submitted to the CGI script because the HTML form submit itself
has no -onSubmit handler.

THank you!
 
B

Brian Kerrick Nickel

I used -onSubmit with the HTML form tab, but I am not sure how to
check which button was pressed, i.e., how do I reference submit1,
submit2 button in my logic of:
if submit1 then
if all textfields have been completed then
return true
else
return false

I also tried to used -onClick with each Submit button, but returning
false if all textfields have not been filled still causes the form to
get submitted to the CGI script because the HTML form submit itself
has no -onSubmit handler.

You could do something along the lines of:

var canSubmit;

...

<form onsubmit="return canSubmit;">

<submit onclick="canSubmit = validator1( );" />
 
M

Michael Hall

try to write a function that will check to see
if the forms are blank or not when the button is pressed.

Can be done with one function or 2 functions. One woulds check all
alternatively create 2 and assign one to each button.
 
C

Chris Riesbeck

Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?

I used -onSubmit with the HTML form tab, but I am not sure how to
check which button was pressed, i.e., how do I reference submit1,
submit2 button in my logic of:
if submit1 then
if all textfields have been completed then
return true
else
return false

I also tried to used -onClick with each Submit button, but returning
false if all textfields have not been filled still causes the form to
get submitted to the CGI script because the HTML form submit itself
has no -onSubmit handler.

Try using both methods. Have the submit button's onclick() methods
set a global status variable to the result of their check of the
text fields. Have the form's onsubmit() method check that variable.

If you want to have an error message that varies depending
on which button was clicked, have the onclick() methods store
an error message if there's a problem. Otherwise they store
null. The form's onsubmit() would display the error
message, if any, and return false, otherwise return true.
 
L

Lee

Syed Ali said:
Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?

You can't check that for sure, because you don't know that the visitor
has JavaScript enabled. If they have JavaScript enabled, you can check
it for their convenience, but you also have to be able to validate on
the server side, so you also need to provide a way for the server to
know which button was pressed.

This works on the client side, and also sends enough information to the
server to allow it to perform the same sort of validation.

Note that I'm taking your specification literally to mean that if the
second button is pressed, that *only* the first field should be filled:


<html>
<head>
<script type="text/javascript">
function validate(f,whichCase){
var textfieldCount=0;
var hasValueCount=0;
var firstHasValue=0;
for(var i=0;i<f.elements.length;i++){
if(f.elements.type=="text"){
textfieldCount++;
if(-1!=f.elements.value.search(/\S/)){
hasValueCount++;
if(textfieldCount==1){
firstHasValue++;
}
}
}
}
if(whichCase=="button1"){ // all must have values
if(hasValueCount==textfieldCount){
return true;
}else{
alert ("all fields must have values.");
return false;
}
}else{ // only first may have value
if((hasValueCount==1)&&(firstHasValue)){
return true;
}else{
alert("the first field (only) must have a value");
return false
}
}
}
</script>
</head>
<body>
<form onsubmit="return validate(this,whichPressed)">
<input><input><input><input>
<input name="whichSubmit" type="submit" value="button1"
onclick="whichPressed=this.value">
<input name="whichSubmit" type="submit" value="button2"
onclick="whichPressed=this.value">
</form>
</body>
</html>
 
E

Evertjan.

Lee wrote on 16 dec 2003 in comp.lang.javascript:
<input name="whichSubmit" type="submit" value="button1"
onclick="whichPressed=this.value">
<input name="whichSubmit" type="submit" value="button2"
onclick="whichPressed=this.value">

If you want the texts on the buttons to be the same, do:


<input name="button1" type="submit" value="Submit me"
onclick="whichPressed=this.name">
<input name="button2" type="submit" value="Submit me"
onclick="whichPressed=this.name">
 
@

@SM

Syed Ali a ecrit :
Hello,

I have 1 HTML form with 4 submit buttons and 10 textfield entry areas.

If submit button1 is pressed I need to make sure that all 10 textfield
entries have been filled before submitting the form.

If submit button2 is pressed I need to make sure that only textfied1
is filled before submitting the form.

How do I go about doing the above check?

<script type="text/javascript"><!--
function verifOne(nameOfField){
if(nameOfField.value=='') {
alert(nameOfField.name+' not filled');
nameOfField.focus();
return false;
}
else return true;
}
function verifAll(MyForm){
ok=0;
for(var i=0;i<MyForm.length;i++) {
if(MyForm.elements.type=="text")
if(!(verif1(MyForm.elements)))
{ ok=1; return; }
}
if(ok==0) return true;
else return false;
}
// --></script>

<form action="blabla" etc>
<input type=button value="Submit one field"
onclick="if(verifOne(this.form.Firstname)) this.form.submit();">
<input type=button value="Submit All"
onclick="if(verifAll(this.form)) this.form.submit();">
</form>

Not tried !

-- -----
@SM
move away *OTEZ-MOI* from my reply url
 
S

Syed Ali

Thank you to everyone who responded.

What worked me for me is setting a global var with -onlick on each
submit button and then checking the value of that global var in
-onSubmit for the form.

For example, submit button1 -onclick sets global var
whichButton=button1.
And in -onSubmit function, I check the value of whichButton, if it is
button1 then I know that all textfields need to be filled, if it is
button2 then I make sure that only the 1 needed textfield is filled.

Thanks again.
 
T

Thomas 'PointedEars' Lahn

Syed said:
What worked me for me is setting a global var with -onlick on each
submit button and then checking the value of that global var in
-onSubmit for the form.

For example, submit button1 -onclick sets global var
whichButton=button1.
And in -onSubmit function, I check the value of whichButton, if it is
button1 then I know that all textfields need to be filled, if it is
button2 then I make sure that only the 1 needed textfield is filled.

There is no need to spoil namespaces. Use `whichbutton' as a property
of a DOM object, e.g. the `HTMLFormElement' object itself. Or use the
`value' property of a input[type="hidden"] `HTMLInputElement' object
instead.
[Top post]

Please do not waste scarce resources.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top