Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I catch an error before its an error? 2

Status
Not open for further replies.

leadman

Programmer
Jun 11, 2001
177
US
Hi all,

I have a form that contains a file input that gets processed by a cffile that accepts only jpg and gif files. If the user tries to upload another type of file an error is thrown and they are taken to my ugly boilerplate error template that catches all errors. I would much rather just show them some text on the page they are on that reads "You may only upload files types of jpg or gif." How can I do this?
 
try using javascript to validate the file type. Use the onsubmit event of the form. Use javascritpt to check for a .jpg or .gif. if you need the script syntax, i can find it for you. I have used it before.
The only dumb questions are the ones that are never asked
 
This is the code that i have used to do the same thing:

function validateUpload() {
var extension
imgVal = document.upload.ImgUp.value;
if (document.upload.ImgUp.value == "") {
alert("Please enter a file to upload.")
document.upload.ImgUp.focus();
return false;
}
extension = imgVal.substring(imgVal.indexOf('.'),imgVal.length)
extension = extension.toLowerCase();
alert(extension);
if ((extension == ".gif") || (extension == ".jpg")) {

} else {
alert("The file type that you are attempting to upload is invalid.\nYou are only allowed to upload .gif and .jpg files.")
document.upload.ImgUp.focus()
return false;
}
}
 
Another way would be to use <cftry> and <cfcatch> to grab the error. I'd go with arperry's javascript solution -- that way it is stopped before it even makes the server.

Tim P.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top