简体   繁体   中英

a condition for upload file only with specific format

How can I define a condition in a form input for uploading an image that user can just upload image with a specific format?

<div class="form-group">

    <div class="leftbox">
         <div class="col-sm-12" align="right">
              <input type="file" name="filedata" style="width:250px; background:none; " name="mipic"/>
                .jpeg,.jpg,.png
              </div>
         </div>
</div><br/>
<input type="file" name="imagefilename" accept="image/x-png, image/gif, image/jpeg" />

you can validate it by using the following JavaScript function.

function imageType() {
    var image = document.getElementById("Image").value;
    var extension;
    if (image == "") {
        alert("Upload a File With jpeg/jpg/png extension");
        return false;
    }
    else 
    {
        extension=image.split(".");
        if (extension[1] == "jpeg" ||extension[1] == "jpg" ||extension[1] == "png")
            return true;
        else {
            alert("File with " + image.split(".")[1] + " is not supported");
            return false;
        }
    }
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM