[code]
<script>
function xd(oForm)
{
var ext = "*.jpg,*.gif";
s = oForm.f.value;
s = s.substr(s.lastIndexOf("."));
if(ext.indexOf("*"+s)==-1)
{
alert("不允许"+s)
return false
}
}
</script>
<form onsubmit="return xd(this)">
<input type=file name=f>
<input type=submit>
</form>[/code]
<script>
function xd(oForm)
{
var ext = "*.jpg,*.gif";
s = oForm.f.value;
s = s.substr(s.lastIndexOf("."));
if(ext.indexOf("*"+s)==-1)
{
alert("不允许"+s)
return false
}
}
</script>
<form onsubmit="return xd(this)">
<input type=file name=f>
<input type=submit>
</form>[/code]
本文介绍了一种使用JavaScript进行客户端表单验证的方法,确保用户上传的文件类型仅限于图片格式(.jpg 和 .gif)。通过自定义函数 xd() 实现了对文件扩展名的检查,并在提交表单前进行验证。
1万+

被折叠的 条评论
为什么被折叠?



