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

Which Submit button was pressed?

P: n/a
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!
Jul 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
> 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( );" />
Jul 20 '05 #2

P: n/a
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.

al**@paul.rutgers.edu (Syed Ali) wrote in message news:<30*************************@posting.google.c om>...
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!

Jul 20 '05 #3

P: n/a
al**@paul.rutgers.edu (Syed Ali) wrote in message news:<30*************************@posting.google.c om>...
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.
Jul 20 '05 #4

P: n/a
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[i].type=="text"){
textfieldCount++;
if(-1!=f.elements[i].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>

Jul 20 '05 #5

P: n/a
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">
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #6

P: n/a
@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[i].type=="text")
if(!(verif1(MyForm.elements[i])))
{ 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
Jul 20 '05 #7

P: n/a
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.

al**@paul.rutgers.edu (Syed Ali) wrote in message news:<30*************************@posting.google.c om>...
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!

Jul 20 '05 #8

P: n/a
Syed Ali wrote:
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
Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.