开发者

Client-side checking of a file input have a correct image file format

I wonder if there is a way of checking correct fileformat and size of an image using javascript?

I have a signup form and it is mandatory to upload an image before using the application:

开发者_如何学C
<form action='signUp.php' method POST>
    choose your image: <input type='file'></input>
</form>


You won't be able to check the file size with just javascript, but you should be able to check the file format of the file selected by simply grabbing the value of that input field and grabbing the last part. Like so, say you have this tag in your HTML:

<input type="file" id="afile" />

In your javascript you could have a function like this, and refer to it when the form is submitted:

function check_filetype(){
    var filename = document.getElementById('afile').value;
    return filename.substr(filename.lastIndexOf('.'));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜