Member Avatar for noamwegner

Does anyone know if i can write a generic javascript function that check the file extension
of the uploaded files and if the extension is incorrect erases the value
because the user upload's more then one file i want to get the ID from the form
something like:

fileselectedchange(obj) { var fileEXT = obj.substring(obj.indexOf('.')+1, obj.length); if (fileEXT.toUpperCase() != 'XLS'){ alert('Please Select Only XLS File'); /* erasing the file input text geting the name from "obj" */ /* something like document.getelemntbyname(obj.name).value = "";*/ return; } } <input type="file" name="firstXLS" onchange="fileselectedchange(this);" />

sorry if my english is not the best
thank you

Member Avatar for Luckychap

'value' property is read-only. But there a work around you can use:

<html> <head> </head> <script> function fileselectedchange(obj) { var fName = obj.value; var fileEXT = fName.substring(fName.indexOf('.') + 1, fName.length); if (fileEXT.toUpperCase() != 'XLS'){ // Reset the innerHTML with the initial one document.getElementById('input1').innerHTML = divInnerHTML; return false; } } </script> <body> <div id="input1"> <input id="fileInput" type="file" name="firstXLS" onchange="fileselectedchange(this);"/> </div> <script> // Store the initial innerHTML for the div. var divInnerHTML = document.getElementById('input1').innerHTML; </script> </body> </html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.