PDA

View Full Version : Form Validation - File Upload Check for Extensions


lyra
11-01-2002, 02:41 PM
I am scripting an application that allows file uploads via a web form to the server. I would like to client-side validate the extension of the file to be uploaded before it is uploaded to the server. Only .pdf, .doc, and .txt extensions will be allowed.

I have this script which works wonderfully to validate one extension but don't know enough JavaScript to make this into a longer OR statement so that if looks to see if the file has EITHER .pdf, .doc, OR .txt extensions before it returns an error:

var i = sFileName.lastIndexOf(".") ;
if (i == -1 || sFileName.substring(i,sFileName.length).toLowerCase() != ".pdf")
{
alert ("The selected file does not have '.pdf' extension.") ;
return ;

Where sFileName is the file being uploaded.

Thanks so much in advance for any help offered. Let me know if you need further information which I've omitted from this query.

Lyra

beetle
11-01-2002, 02:56 PM
My validator, fValidate (http://www.peterbailey.net/fValidate/site.php) has got ya covered (http://www.peterbailey.net/fValidate/site.php?page=types#file)

:D