RAHULINDIA122

image validation

Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. html form
  2. ------------------------------------------------------------------------------------------------------
  3. <form onsubmit="return false"  class="form-horizontal" name="ads_post" id="post_form" enctype="multipart/form-data" method="post">
  4. <div class="form-group">
  5.       <label class="control-label col-sm-2" >Image</label>
  6.       <div class="col-sm-10">
  7.       <table>
  8.         <tr>
  9.         <td><input type="file"  class="file" name="file" id="input-upload-img1"></td>
  10.        
  11.         </tr>
  12.      </table>
  13.       </div>
  14.       <span class="img_error"></span>
  15.     </div>
  16. <div class="form-group">        
  17.                       <div class="col-sm-offset-2 col-sm-10">
  18.                         <button type="submit" class="btn btn-primary" name="ad_submit">Submit</button>
  19.                       </div>
  20.                     </div>
  21. </form>
  22.  
  23. --------------------------------------------------------------------------------------------------------
  24. validate.js
  25. --------------------------------------------------------------------------------------------------------
  26. $(document).ready(function(){
  27.  
  28.    function verify_img(img){
  29.    
  30.     $(".img_error").hide();
  31.     if(img == ""){
  32.     $(".img_error").show();
  33.     $(".img_error").html("plz Choose an image");
  34.     return false;
  35.     }
  36.     else{
  37.     $(".img_error").show();
  38.     $(".img_error").html("validating....");
  39.     $.ajax({
  40.    
  41.             url:"classes/process.php",    
  42.             type: "POST",            
  43.             data: {check_img:1,img:img},
  44.             success: function(data)  
  45.             {
  46.    
  47.     alert(data);
  48.    
  49.     },error: function(req, status, error) {
  50.           // alert('Error: ' + req.status);
  51.             console.log(error);
  52.             console.log();
  53.             }
  54.    
  55.      })
  56.     }
  57.    
  58. }
  59.  
  60.  
  61. $("#input-upload-img1").focusout(function(){
  62.         var img=$("#input-upload-img1").val();
  63.         verify_img(img);
  64.  
  65.           })
  66.          
  67.  
  68.  
  69. })
  70.  
  71.  
  72. ---------------------------------------------------------------------------------------------------
  73. process.php
  74. ---------------------------------------------------------------------------------------------------
  75.  
  76. <?php
  77.  
  78. include "dbcon.php";
  79.  
  80. class DataOperation extends database{
  81.  
  82.  
  83.  
  84. public function verify_img($file){
  85.    
  86.     $regexp ="/\.(jpe?g|png)$/i";
  87.     return (!preg_match($regexp,$title)) ? "Invalid_extension" : 'ok';
  88.      
  89.     }
  90.  
  91. }
  92.  
  93. $obj = new DataOperation;
  94.  
  95. if(isset($_FILES['file']['check_img'])){
  96.      $file=$_FILES['file']['img'];
  97.      echo $data=$obj->verify_img($file);
  98.      exit();
  99.  }
  100.  
  101.  
  102.  ?>
Add Comment
Please, Sign In to add comment