javascript如何判断form表单值是否是图片类型

JavaScript019

javascript如何判断form表单值是否是图片类型,第1张

<script language="javascript">

function CheckForm()

{

if(document.w.certinfo.value=="")

{

alert("证书描述不能为空!")

return false

}

else if(document.w.files.value=="")

{

alert("证书图片不能为空!")

return false

}else {

var file_value = document.getElementById('file').value

alert(isJpg(file_value))

return isJpg(file_value)

}

return true

}

function chkimg(inp)

{

var img=null

if(img)img.removeNode(true)

img=document.createElement("img")

img.attachEvent("onerror",notimg())

img.src=inp

}

function notimg()

{

alert("您插入的不是图片,请重新选择插入!")

}

function isJpg(v) {

if(!v) return

//判断是否为图片的数组,你自己往里面加扩展名吧

var jpg = ['jpg','png']

v = v.split('.')

v = v[v.length - 1]

for(var i in jpg) {

if(jpg[i] == v) {

return true

}

}

return false

}

</script>

网页中表单代码如下:

<form name="w" action="user/upload_cert_success.html" method="post" onSubmit="return CheckForm()" ENCTYPE="multipart/form-data">

<div class="add">

<ul class="ty">证书描述:</ul>

<ul class="cert"><input type="text" name="certinfo" maxlength="100" value="资质认定计量认证证书" /></ul>

</div>

<div class="add1">

<ul class="ty">证书图片:</ul><ul class="certpic"><input type="file" id="file" name="files" /></ul>

</div>

<button type="submit">上传证书</button>

</form>

var g=document.getElementById("div1")

var img=new Image()

img.src=g.src

if(img.readyState == 'complete')

{

alert("有图片且图片已加载完成")

} else {

alert('没有图片!')

}