练习JavaScript判断上传文件后缀名

<script type = text/javascript>

function jiance(filename)
{
    var pic = ["jpg","png","gif"];
    var indx = filename.lastIndexOf(".");
    var ext = filename.substr(indx+1);
    for (var a = 0;a < pic.length; a++ )
    {
        if (ext == pic[a])
        {
            document.write("文件名合法");
            break;
        }else
        {
            document.write("文件不合法");
            break;
        }
    }
}
</script>
</head>
<body>
<from method = "post" enctype = "multipart/form-data">
上传图片<input type = "file" name = "uploadfile" onchange = "jiance(this.value)">
</from>
</body>
</html>
posted on 2015-12-31 08:48  土dou  阅读(313)  评论(0编辑  收藏  举报