That still calls both of them. I'd modify your solution like this:
function checkBoth(frm,field) {
return (LimitAttach(frm,field) && ProgressBar(field));
}
So that it breaks out early if LimitAttach returns false.
Main Topics
Browse All TopicsI have 2 Javascript functions that I want to perform when a form is submitted. The first is a check to see if a valid extension exists on a file input field. If that comes back good, I want the second to be excecuted. I would like to keep the 2nd function as the main OnSubmit field in the FORM. Here are the functions
Function 1 (check extension of field.)
<script language="JavaScript">
//Function to check for valid file extension
extArray = new Array(".jpg", ".png", ".bmp", ".gif", ".wmv");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\
ext = file.slice(file.indexOf(".
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) return true;
else
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
return false;
}
// End -->
</script>
And if that one comes back ok, I want it to procede with Function 2:
function ProgressBar(form){
//ASP script handling progress window
var ProgressScript
ProgressScript = 'progress-smooth.asp'
//Progress window parameters
var pp = 'toolbar=no,location=no,di
pp+=',scrollbars=no,resiza
//1. Get unique UploadID
var UploadID
UploadID = Math.round(Math.random() * 0x7FFFFFF0)
//2. Add upload ID to form action URL
var action = form.action;
if ('' == action) action = ''+document.location;
action = AddToQuery(action, 'UploadID', UploadID);
form.action = action
//alert(form.action)
//3. Open progress window with the same UploadID
var ProgressURL
ProgressURL = ProgressScript + '?UploadID=' + UploadID
var v = window.open(ProgressURL,'_
return true;
};
I want my <FORM> tag to look like this:
<form name="file_upload" method="POST" ENCTYPE="multipart/form-da
So basically just roll the IF/THEN function 1 into function 2. Pretty simple, huh!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ooops. I'm still on my first cup of coffee ^^
Thanks for pointing out my bads, dbritt and mplungjan
I was for some strange reason thinking the function was called from the file field...
If there's only one filefield you can do this. This will only call ProgressBar() if LimitAttach() returns true.
function checkBoth(frm) {
var retVal = (LimitAttach(frm.elements[
return retVal;
}
Still not working...here is what i have:
<SCRIPT>
function checkBoth(frm) {
var retVal = (LimitAttach(frm.elements[
return retVal;
}
//Open window with progress bar.
//pair upload window and progress window (using UploadID).
function ProgressBar(form){
//check file sizes.
//ASP script handling progress window
var ProgressScript
ProgressScript = 'progress-smooth.asp'
//Progress window parameters
var pp = 'toolbar=no,location=no,di
pp+=',scrollbars=no,resiza
//1. Get unique UploadID
var UploadID
UploadID = Math.round(Math.random() * 0x7FFFFFF0)
//2. Add upload ID to form action URL
var action = form.action;
if ('' == action) action = ''+document.location;
action = AddToQuery(action, 'UploadID', UploadID);
form.action = action
//alert(form.action)
//3. Open progress window with the same UploadID
var ProgressURL
ProgressURL = ProgressScript + '?UploadID=' + UploadID
var v = window.open(ProgressURL,'_
return true;
};
//Adds value and its name to querystring
function AddToQuery(q, valname, val){
if (q.indexOf('?')<0) {
q += '?'
} else {
var pv = q.indexOf(valname+'=');
if (pv >= 0){
var amp = q.indexOf('&', pv);
if (amp<0) {
q = q.substr(0, pv)
} else {
q = q.substr(0, pv) + q.substr(amp+1) + '&'
}
} else {
if (q.substr(q.length-1)!='?'
};
};
q += valname + '=' + val
return q
};
//Function to check for valid file extension
extArray = new Array(".jpg", ".png", ".bmp", ".gif", ".wmv");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\
ext = file.slice(file.indexOf(".
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) return true;
else
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
return false;
}
</script>
In the form I have:
onSubmit="return checkBoth(this);">
<script language="JavaScript">
//Function to check for valid file extension
var extArray = new Array(".jpg", ".png", ".bmp", ".gif", ".wmv");
function LimitAttach(file) {
var fileName = file.value
if (!fileName) return false;
ext = fileName.substring(fileNam
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) return true
}
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
file.focus();
return false;
}
//Adds value and its name to querystring
function AddToQuery(q, valname, val){
if (q.indexOf('?')<0) {
q += '?'
} else {
var pv = q.indexOf(valname+'=');
if (pv >= 0){
var amp = q.indexOf('&', pv);
if (amp<0) {
q = q.substr(0, pv)
} else {
q = q.substr(0, pv) + q.substr(amp+1) + '&'
}
} else {
if (q.substr(q.length-1)!='?'
};
};
q += valname + '=' + val
return q
};
function ProgressBar(form){
//ASP script handling progress window
var ProgressScript
ProgressScript = 'progress-smooth.asp'
//Progress window parameters
var pp = 'toolbar=no,location=no,di
pp+=',scrollbars=no,resiza
//1. Get unique UploadID
var UploadID
UploadID = Math.round(Math.random() * 0x7FFFFFF0)
//2. Add upload ID to form action URL
var action = form.action;
if ('' == action) action = ''+document.location;
action = AddToQuery(action, 'UploadID', UploadID);
form.action = action
//alert(form.action)
//3. Open progress window with the same UploadID
var ProgressURL
ProgressURL = ProgressScript + '?UploadID=' + UploadID
var v = window.open(ProgressURL,'_
return true;
}
function checkBoth(frm) {
return LimitAttach(frm.fileField)
}
</script>
<form name="file_upload" method="POST" ENCTYPE="multipart/form-da
<input type="file" name="fileField">
<input type="submit">
</form>
Still acting up. Here is my exact code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<TITLE>WTHI File Upload Form</TITLE>
<STYLE TYPE="text/css"><!--TD {font-family:Arial,Helveti
<meta name="robots" content="noindex,nofollow"
</HEAD>
<BODY BGColor=white>
<form name="file_upload" method="POST" ENCTYPE="multipart/form-da
<table width="450" border="0" cellspacing="2" cellpadding="2">
<tr>
<td colspan="2"><font face="Arial" size=2>Please use the form below to upload your video to News10. Videos must be in Windows Media Format (WMV) and be less than 20MB in size to be accepted. After your video is uploaded, News10 will be notified.<br><br></td>
</tr>
<tr>
<td><span class="style1">Choose Video:</span></td>
<td><input type="file" name="Video"></td>
</tr>
<tr>
<td class="style1">Short Description:</td>
<td><input Name=Description Size=40></td>
</tr>
<tr>
<td> </td>
<td><input Name=SubmitButton Value="Upload File" Type=Submit Style="background-color:#3
</tr>
</table>
</form>
<script language="JavaScript">
//Function to check for valid file extension
var extArray = new Array(".jpg", ".png", ".bmp", ".gif", ".wmv");
function LimitAttach(file) {
var fileName = file.value
if (!fileName) return false;
ext = fileName.substring(fileNam
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) return true
}
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
file.focus();
return false;
}
function AddToQuery() { alert('not implemented') }
function ProgressBar(form){
//ASP script handling progress window
var ProgressScript
ProgressScript = 'progress-smooth.asp'
//Progress window parameters
var pp = 'toolbar=no,location=no,di
pp+=',scrollbars=no,resiza
//1. Get unique UploadID
var UploadID
UploadID = Math.round(Math.random() * 0x7FFFFFF0)
//2. Add upload ID to form action URL
var action = form.action;
if ('' == action) action = ''+document.location;
action = AddToQuery(action, 'UploadID', UploadID);
form.action = action
//alert(form.action)
//3. Open progress window with the same UploadID
var ProgressURL
ProgressURL = ProgressScript + '?UploadID=' + UploadID
var v = window.open(ProgressURL,'_
return true;
}
function checkBoth(frm) {
return LimitAttach(frm.fileField)
}
</script>
</CENTER>
</Div>
</BODY></HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<TITLE>WTHI File Upload Form</TITLE>
<STYLE TYPE="text/css"><!--TD {font-family:Arial,Helveti
<meta name="robots" content="noindex,nofollow"
<script language="JavaScript">
//Function to check for valid file extension
var extArray = new Array(".jpg", ".png", ".bmp", ".gif", ".wmv");
function LimitAttach(file) {
var fileName = file.value
if (!fileName) return false;
ext = fileName.substring(fileNam
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) return true
}
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
file.focus();
return false;
}
//Adds value and its name to querystring
function AddToQuery(q, valname, val){
if (q.indexOf('?')<0) {
q += '?'
} else {
var pv = q.indexOf(valname+'=');
if (pv >= 0){
var amp = q.indexOf('&', pv);
if (amp<0) {
q = q.substr(0, pv)
} else {
q = q.substr(0, pv) + q.substr(amp+1) + '&'
}
} else {
if (q.substr(q.length-1)!='?'
};
};
q += valname + '=' + val
return q
};
function ProgressBar(form){
//ASP script handling progress window
var ProgressScript
ProgressScript = 'progress-smooth.asp'
//Progress window parameters
var pp = 'toolbar=no,location=no,di
pp+=',scrollbars=no,resiza
//1. Get unique UploadID
var UploadID
UploadID = Math.round(Math.random() * 0x7FFFFFF0)
//2. Add upload ID to form action URL
var action = form.action;
if ('' == action) action = ''+document.location;
action = AddToQuery(action, 'UploadID', UploadID);
form.action = action
//alert(form.action)
//3. Open progress window with the same UploadID
var ProgressURL
ProgressURL = ProgressScript + '?UploadID=' + UploadID
var v = window.open(ProgressURL,'_
return true;
}
function checkBoth(frm) {
return LimitAttach(frm.Video) && ProgressBar(frm);
}
</script>
</head>
</HEAD>
<BODY BGColor=white>
<form name="file_upload" method="POST" ENCTYPE="multipart/form-da
<table width="450" border="0" cellspacing="2" cellpadding="2">
<tr>
<td colspan="2"><font face="Arial" size=2>Please use the form below to upload your video to News10. Videos must be in Windows Media Format (WMV) and be less than 20MB in size to be accepted. After your video is uploaded, News10 will be notified.<br><br></td>
</tr>
<tr>
<td><span class="style1">Choose Video:</span></td>
<td><input type="file" name="Video"></td>
</tr>
<tr>
<td class="style1">Short Description:</td>
<td><input Name=Description Size=40></td>
</tr>
<tr>
<td> </td>
<td><input Name=SubmitButton Value="Upload File" Type=Submit Style="background-color:#3
</tr>
</table>
</body>
</html>
Glad to help. Thanks for the points.
Note that if you want to make sure that the progressBar function is only called if limitAttach returns true you might want to use my second suggestion, here rewritten to match your field name and without the ) typo.
function checkBoth(frm) {
var retVal = (LimitAttach(frm.elements[
return retVal;
}
When declaring retVal it will call the LimitAttach function and then call ProgressBar if LimitAttach returns true, or set retVal to false if not.
Just to clarify
1. I did not base my suggestion on BraveBrains idea - it did not first execute one and if that came back ok, execute the second. DBritt had the better solution not to call the second function if the first was false. He however also failed to notice the passing of a roque field.
2. When I tested your snippets, I had to invent a field called fileField and a function called AddToQuery - if you scroll back you will see that I had a
function AddToQuery() { alert('not implemented') }
because I did not know what it did.
Then when you posted #19531649: Still acting up.
you had that code with the incorrect field name in it, hence my comment
change
return LimitAttach(frm.fileField)
to
return LimitAttach(frm.Video) && ProgressBar(frm);
and put your addtoquery back.
3. I also changed the test for extension to be much simpler and easier to understand.
Business Accounts
Answer for Membership
by: BraveBrainPosted on 2007-07-20 at 07:30:52ID: 19531327
function checkBoth(frm,field) {
ta" OnSubmit="return checkBoth(this.form,this); ">
ng,image/b mp,image/g if,video/x -ms-wmv"
var mimeOk = LimitAttach(frm,field);
var progrBar = ProgressBar(field);
return (mimeOk && progrBar);
}
<form name="file_upload" method="POST" ENCTYPE="multipart/form-da
To only accept certain mimetypes you can also add the ACCEPT attribute in your file tag:
ACCEPT="image/jpeg,image/p