"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Thursday, August 5, 2010

FileUpload Javascript Validation

Hi friends,
It's better to avoid validation through server side even we can do through client side.
this code is for fileupload validation . It wil accept only ".xls", ".xlsx", ".XLS",".XLSX" 
Fileuploader Id is FileUpload1 

Code
    <script type="text/javascript">        function CheckFileExtension() {             var ctrlUpload = document.getElementById('<%=FileUpload1.ClientID%>'); //Does the user browse or select a file or not.             if (ctrlUpload.value == '') {alert("Select a file first!");ctrlUpload.focus();return false;} //Extension List for validation. Add your required extension here with comma separator.             var extensionList = new Array(".xls", ".xlsx", ".XLS", ".XLSX"); //Get Selected file extension             var extension = ctrlUpload.value.slice(ctrlUpload.value.indexOf(".")).toLowerCase(); //Check file extension with your required extension list.             for (var i = 0; i < extensionList.length; i++) { if (extensionList[i] == extension) return true; } alert("You can only upload an Excel File Type i.e.\n" + extensionList.join(" or\n")); ctrlUpload.focus(); return false;         } </script>
now call the function CheckFileExtension() on button onclientclick event it solved 
have a nice day 'n happy coding :)
 JavaScript: The Good PartsJavaScript: The Definitive GuideObject-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications and libraries
 

No comments: