FileType Validations JavaScript

To upload only specific type of files from users, we need to restrict them to upload only specific type of files. Following example will demonstrates the same for only uplod of word files.

<title>JavaScript Validation</title>
<script type="text/javascript">
    function Checkfiles()
        var fup = document.getElementById('filename');
        var fileName = fup.value;
        var ext = fileName.substring(fileName.lastIndexOf('.') + 1);

    if(ext =="doc" || ext=="docx")
        return true;
        alert("Upload word files only");
        return false;
<form name="xx" action="second.php" method="post" enctype="multipart/form-data" 
onsubmit="return Checkfiles();"">
  <input type="file" name="file_uploading" id="filename">
  <input type="submit" value="Submit" name="uploadfile">

You may also like...