JS判断是否为图片后缀名

// 增加一个名为 IsPicture 的函数作为
    // String 构造函数的原型对象的一个方法。
    String.prototype.IsPicture = function()
    {
        
//判断是否是图片 - strFilter必须是小写列举
        var strFilter=".jpeg|.gif|.jpg|.png|.bmp|.pic|"
        
if(this.indexOf(".")>-1)
        {
            
var p = this.lastIndexOf(".");
            
//alert(p);
            //alert(this.length);
            var strPostfix=this.substring(p,this.length) + '|';        
            strPostfix = strPostfix.toLowerCase();
            
//alert(strPostfix);
            if(strFilter.indexOf(strPostfix)>-1)
            {
                
//alert("True");
                return true;
            }
        }        
        
//alert('False');
        return false;            
    }

//<!--前台HTML文件//-->//

//预览图片
        function showImg()
        { 
            document.getElementById(
"imgPic").removeAttribute("height");
            
if(document.getElementById("spnPic").style.display!="none")document.getElementById("spnPic").style.display='none';
            
if(document.getElementById("Image").value.trim()!='')
            {    
                
//IsPic();
                document.getElementById("imgPic").style.display='';
                document.getElementById(
"imgPic").src=document.getElementById("Image").value.trim();    
                
if(document.getElementById("imgPic").height>150) document.getElementById("imgPic").height=150;
                
//if(document.getElementById("imgPic")>300) document.getElementById("imgPic").width=300;            
            }            
            
else
            {
                document.getElementById(
"imgPic").style.display='none';
                document.getElementById(
"imgPic").src='';
            }        
        }
        
        
//图片验证
        function IsPic()
        {
            
if(document.getElementById("Image").value.trim()!='')
            {
                
if(document.getElementById("Image").value.IsPicture()==false)
                {
                    alert('请选择正确的图片路径!');
                    document.getElementById(
"Image").focus();
                    document.getElementById(
"Image").select();
                }
            }
        }
posted @ 2006-03-01 22:06  blueKnight  Views(13765)  Comments(2Edit  收藏  举报