2014-12-05 30 views
0

如何驗證圖像像素? 以下是我的代碼,但只適用於圖像類型和大小。如何驗證圖像像素?

<!DOCTYPE html> 
    <html> 
    <head> 
    <title>Image Validation</title> 
    <meta charset="utf-8"> 
    <title>jQuery validation plug-in - comment form example</title> 
    <script src="lib/jquery.js"></script> 
    <script src="js/file-validator.js" type="text/javascript" charset="utf-8"></script> 
    <script src="js/app.js"></script> 
    <link rel="stylesheet" href="css/cssval.css"> 
    <body> 
     <fieldset> 
      <legend>Image</legend> 
      </p> 
       </li> 
       <li>Choose an image file: 
        <input type='file' class='demo' data-type='image' data-max-size='2mb' /> 
       </li> 
     </fieldset> 

    </body> </html> 

</head> 
+0

我試着去添加此但沒有運氣:document.getElementById('your_form')。onsubmit = function(){ var uploadImage = document.getElementById('upload_field').value; var img = new Image(); img.src = uploadImage; img.onload = function(){ if(this.width> 200 || this.height> 400)alert('Your image is too big,it must be less than 200x400'); } } } – 2014-12-05 06:41:07

回答

0

這是與jQuery我thnk你預覽IMG第一任何隱藏或不和預覽後使用該代碼中看到THISJsfiddle和預覽後使用該代碼

$(document).ready(function(){ 
var imgWidth = $('#target').width(); 
var imgHeight = $('#target').height(); 
if(imgWidth > 400 || imgHeight > 200){ 
alert('Your image is too big, it must be less than 200x400'); 
} 
}); 
+0

你是什麼意思隱藏?我會試試這個謝謝。 – 2014-12-05 07:00:34

+0

我的意思是如果你不希望你的用戶的上傳圖像顯示預覽..只是隱藏顯示:沒有那個div – 2014-12-05 08:52:09