jQuery - INPUT type=File , Image FileType Validation options?

后端 未结 6 1865
误落风尘
误落风尘 2020-12-29 09:08

I have the following:


On the server I check to make sure it\

6条回答
  •  旧巷少年郎
    2020-12-29 09:31

    You could use a regular expression:

    var val = $("#user_profile_pic").val();
    if (!val.match(/(?:gif|jpg|png|bmp)$/)) {
        // inputted file path is not an image of one of the above types
        alert("inputted file path is not an image!");
    }
    

    Of course you could add more formats to the regular expression. There are more complex and comprehensive ways to accomplish this, but this method will accomplish the task as long as the image file path inputted ends in a standard image file extension.

提交回复
热议问题