0

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

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Luckychap
0

'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>

Edited by Luckychap: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.