Tuesday, June 10, 2014

Image Extension Validation with JavaScript in PHP

After Completing this Tutorial you will able to do validation regarding image extension. As you may be known to valid extensions which are jpg, jpeg, png, gif.

So create index.php and uploadphoto.php file and write whatever message you want to display on uploadphoto.php because we are about to consider validation part not the database part.

index.php
 <html>  
 <body bgcolor="orange">  
 <?php  
 echo "<form action='uploadphoto.php' method='post' enctype='multipart/form-data' >  
  <b>Select Image:</b> <input type='file' name='image' id='image'><br />  
  <br />  
  <input type='submit' name='Submit' value='Upload' id='button1' onClick='return img_path()'/>  
  </form>"  
 ?>  
 <br/>  
 <script type="text/javascript" language="javascript">  
 function img_path()  
 {  
 var file_path=document.getElementById('image').value;  
 if(file_path.match(".jpeg$")==".jpeg" || file_path.match(".gif$")==".gif" || file_path.match(".GIF$")==".GIF" || file_path.match(".JPEG$")==".JPEG" || file_path.match(".JPG$")==".JPG" ||file_path.match(".jpg$")==".jpg")  
 {  
 return true;  
 }  
 else  
 {  
 alert("Upload .jpg/.gif /.png file only");  
 return false;  
 }  
 }  
 </script>  
 </body>  
 </html>  

Output :
Select Image Screen
Image Extension Validation Screen

No comments:

Popular Posts