I have a file upload in my page. I defined an attribute with name: "filetype".
<input filetype=".jpg|.jpeg|.png" id="attachments" type="file">
I want when I select a file from file upload, with the javascript, I check file type:
function onSelect(e) {
if (!e.files[0].extension.match('/' + $("input[type='file']").attr('filetype') + '/i')) {
alert('file type is wrong');
e.preventDefault();
}
}
Now, when I select a file with .jpg format, e.files[0].extension would be
.jpg
and '/' + $("input[type='file']").attr('filetype') + '/i' is
/.jpg|.jpeg|.png/i
but e.files[0].extension.match('/' + $("input[type='file']").attr('filetype') + '/i') returns
null
and then alert fires. Why? and how I solve this problem? Thanks.