You must Sign In to post a response.
  • Category: JavaScript

    How check multiple (dot) of file name in javascript

    hi,

    I want check before upload a file with extention name. Most I likely to upload
    only pdf file not another file like txt, doc, xls or other. If someone type upload
    file with multiple dot extention. i.e. aabc.pdf.phf or aabc.php.pdf Such type of file
    should not load. only allowed file as like aabc.pdf
  • #767937
    Hi,
    It is easy to get the File Extension using JavaScript.

    String extension = "";
    int i = fileName.lastIndexOf('.');
    int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
    if (i > p) {
    extension = fileName.substring(i+1);
    }

    But you should keep in mind, this code will work for files like Document.txt,Document.pdf,Document.xl
    but i wont work for the formats like,
    document.war.xml or document.zip.doc etc.,
    Hope this will be helpful.

    Thanks,
    Mani

  • #767938
    Hai Chandrasekhar,
    You can use simple regex to validate the extension of the file which is going to be upload:

    var val = $(this).val().toLowerCase();
    var regex = new RegExp("(.*?)\.(pdf)$");
    if(!(regex.test(val))) {
    $(this).val('');
    alert('Please select only pdf file format');
    }

    Now in this you don't need to check multiple dots as it will validate the file abc.pdf.pdf as valid while abc.pdf.xml as invalid.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767946
    It's possible to check only the file extension, but user can easily rename virus.exe to virus.jpg and "pass" the validation.
    For what it's worth, here is the code to check file extension and abort if does not meet one of the valid extensions: (choose invalid file and try to submit to see the alert in action)
    see below snippet

    var _validFileExtensions = [".pdf"];
    function Validate(oForm) {
    var arrUpFls = oForm.getElementsByTagName("input");
    for (var i = 0; i < arrUpFls.length; i++) {
    var flUpload = arrUpFls[i];
    if (flUpload.type == "file") {
    var szFileName = flUpload.value;
    if (szFileName.length > 0) {
    var blnValid = false;
    for (var j = 0; j < _validFileExtensions.length; j++) {
    var sCurExtension = _validFileExtensions[j];
    if (szFileName.substr(szFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
    blnValid = true;
    break;
    }
    }

    if (!blnValid) {
    alert("Sorry, " + szFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
    return false;
    }
    }
    }
    }

    return true;
    }

    //call it from fileupload control (Html control)
    < form onsubmit="return Validate(this);">
    File: < input type="file" name="my file" /><br />
    < input type="submit" value="Submit" />
    < /form>

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments