+ Start a Discussion
pranav shah 40pranav shah 40 

File Extension validation

Hi All,

I Want to give Some Validation to File Extemsion...So that it should accept particular file Format.....

i have Written these
 function check(obj) { 
            var path = obj.value;
            var ext = path.substring(path.lastIndexOf('.') + 1);
            if(ext !="pdf")
            {
                obj.value = null;
                window.alert("File can be only of type : pdf, doc, docx, xls, xlsx, ppt, pptx, pps, ppsx ");
                return false;
            }
     }
but its not Working..i want to give validation for 5-6 file Format...how it will be Written,, Kindly Suggest.
Yury BondarauYury Bondarau
Hi Parnav, 

As far as I see you trying to get file extension wih javascript. Please see the following thread in stackowerflow

http://stackoverflow.com/questions/190852/how-can-i-get-file-extensions-with-javascript

for instance you can use
 
return filename.split('.').pop();

 
Ariel Siler 6Ariel Siler 6
Be adviced that this solution won't work if the file name has more than a single dot.
If you know the expected extension you could use instead filename.replace('.pdf', '');