Pages

Men

rh

4/10/2013

Image Validation using Javascript with .Net

<script language="javascript" type="text/javascript">
 
    //************************
    //Width to resize large images to
    var maxWidth=100;
    //Height to resize large images to
    var maxHeight=100;
    //valid file Types.
     var fileTypes=["bmp","gif","jpg","jpeg","doc","pdf"];
     //the id of the preview image tag
     var outImage="previewField";
     //what to display when the image is not valid
     var defaultPic="Images/myprofile.gif";
   
     // *********
    var globalPic;
    function preview(what)
    {
       var source=what;
       var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
       for(var i=0;i<fileTypes.length; i++)
       if(fileTypes[i]==ext)
       break;
       globalPic=new Image();
       if(i<fileTypes.length)
       globalPic.src=defaultPic;
       else{
       alert("THAT IS NOT A VALID IMAGE\n Please load an image with an extension of one of the following :\n\n"+fileTypes.join(", "));
       return false;
       }
       setTimeout=("applyChanges()",200);
     
     }
     function applyChanges()
     {
      var field=document.getElementById('ctl00$ContentPlaceHolder1$imgGridUser');
      var x=parseInt(globalPic.width);
      var y=parseInt(globalPic.height);
   
      if(x>maxWidth)
      {
        y*=maxWidth/x;
        x=maxWidth;
     
      }
      if(y<maxHeight)
      {
       x*=maxHeight/y;
       y=maxHeight;
     
      }
     field.style.display=(x<1 || y<1)?"none":"";
     field.src=globalPic.src;
     field.width=x;
     field.height=y;
     }
     function ValidateImageUpload()
     {
      var Img=document.getElementById('fleImageUpload');
        if(Img.value=="")
        {
         alert('Select Image File');
         Img.focus();
         return false;
        }
         if(Img.value!="")
         {
          var blvalue=preview(Img.value);
          return blvalue;
        }
        return true;
     
     }
 
  </script>    

No comments :

Post a Comment