使用js检测图片合法性

用js检测图片合法性

<script type=”text/javascript”>
function validate() {
var OK = new Array (‘jpg’, ‘gif’, ‘jpeg’, ‘png’, ‘jpe’, ‘bmp’, ‘tif’);
var filename = document.getElementById(“ELEMENT_ID”).value;
var ext = getExt(filename);
var fileOK = 0;

for (i = 0; i < OK.length; i++) {
if (OK[i] == ext) {
fileOK = 1; // one of the file extensions found
}
}

if (fileOK == 0) {
alert (“The file is not an image file”);
return false;
}
}

function getExt(filename) {
var dot_pos = filename.lastIndexOf(“.”);
if(dot_pos == -1)
return “”;
return filename.substr(dot_pos+1).toLowerCase();
}
</script>
<form action=”ACTION” onsubmit=”return validate()” enctype=”multipart/form-data” method=”post”>
<table class=”forms”>
<tr>
<th><label>Image</label></th>
<td><input id=”ELEMENT_ID” name=”poster[image]” size=”30″ type=”file” />
<input id=”event_poster_image_temp” name=”poster[image_temp]”
type=”hidden” value=”” />
</td>
</tr>
<tr>

<th class=”last”></th>
<td class=”last”>
<input class=”button” name=”commit” type=”submit” value=”Upload image” />
</td>
</tr>
</table>
</form>