[Javascript] Question: Getting 2 functions to work on 1 onclick event
Esther_Strom at hmco.com
Esther_Strom at hmco.com
Wed Aug 8 10:50:58 CDT 2001
Why not have your MM_validateForm() function call Save_onClick() if it
returns the results you want? You have this section:
if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
Why not modify it to:
if (errors) {
alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors=='');
}
else {
Save_onClick();
}
(Not sure of the curly braces; it's been a while and I wasn't looking at
your original that closely. But there's no reason you can't call the save
function from inside the validate function.)
Hope this helps,
Esther
"Neal, Craig"
<CNeal at dthr.ab.ca To: "'javascript at LaTech.edu'" <javascript at LaTech.edu>
> cc:
Sent by: Subject: [Javascript] Question: Getting 2 functions to work on 1
javascript-admin@ onclick event
LaTech.edu
08/08/01 10:42 AM
Please respond to
javascript
Any idea how I can get 2 functions to work on the same onclick event?
The first function is a modified Dreamweaver validation script which uses
alert to pop up a window with information on which form fields have not
been completed.
If all fields are complete then it returns no alert window.
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2];
val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail
address.\n';
} else if (test!='R') {
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (val<min || max<val) errors+='- '+nm+' must contain a number between '
+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n';
}
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
The second function submits the form information for processing to another
page I have.
function Save_onclick() {
SaveForm.Page.value = otherDoc.body.outerHTML;
SaveForm.submit();
}
Both functions work flawlessly by themselves.
However I want MM_validateForm() to be processed first and if it returns no
errors then I want Save_onclick() to run.
This only runs Save_onclick() and never gets to MM_validateForm()
<input type="button" value="Submit Bulletin" name="Save" LANGUAGE=
"javascript"
onclick="return Save_onclick();MM_validateForm
('ndate','','R','title','','R','printdate','','R','email','','R','moreinfo','','R');return
document.MM_returnValue">
and this only runs MM_validateForm() but doesn't run Save_onclick even if
MM_validateForm returns no errors.
<input type="button" value="Submit Bulletin" name="Save" LANGUAGE=
"javascript"
onclick="MM_validateForm
('ndate','','R','title','','R','printdate','','R','email','','R','moreinfo','','R');return
document.MM_returnValue;return Save_onclick()">
I appreciate any assistance on how I write the syntax so that
MM_validateForm() runs first and if no error messages are generated then
Save_onclick() runs.
Thanks so much.
More information about the Javascript
mailing list