Click here to Skip to main content
15,888,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have 2 images in my application.


how to validate images validation in javascript.


1 image is available and 2nd image is not available.


This time how to validation in javascript.
Posted
Comments
Zoltán Zörgő 9-Oct-12 2:19am    
How? What?
You have two file inputs, and you want to check if there are both filled in?
Or what?
kumar2233 9-Oct-12 2:25am    
in my application 2 image src tags.

all the images are comes in database.

sometimes in databases images is null.

My question is 1 img src comes in db and 2nd image not comes in db [ like empty ].

This time how to validate images in javascript.
Zoltán Zörgő 9-Oct-12 2:34am    
So your want to check on client side in javascript, if the image file is available/generated on the server? Why don't you do that on server side? Let's suppose you do it in js, and you find that the image is not sent by the server, what than?
kumar2233 9-Oct-12 2:40am    
Any way can u send any article related to my requirement.

Zoltán Zörgő 9-Oct-12 3:12am    
See my answer. You can copy-paste it directly in a blank html document, and try it out as it is.

1 solution

See this method:

JavaScript
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<script> 
$(function() { // Call when page is fully loaded
    $("img.deferred").each(function(index) { // for each deferred image
        var tochange = this; // this is the image we will change
        var altsrc = $(this).attr('altsrc'); // here we store the actual image url
        var img = $("<img />").attr('src', altsrc).load(function() { //we try to load the actual image in an element
            $(tochange).attr('src', altsrc) // if we succeed, we can use the source for the actual image
        });
    });
});
</script>
Sample usage:<br>
<img class="deferred" src="http://www.nmh.ie/iopen24/images/content_images/other/warning.jpg" altsrc="http://www.jeremynoeljohnson.com/jimages/blogposts/apples01.jpg"><br>
<img class="deferred" src="http://www.nmh.ie/iopen24/images/content_images/other/warning.jpg" altsrc="http://www.jeremynoeljohnson.com/jimages/blogposts/NOTFOUND.jpg">


Notice the two img elements above. Both have a specific class, this makes them different from all others, and that is used by the jquery selector to find only them. The src attribute holds an url of a dummy static image, that is loaded by default. The altsrc (non-standard, custom) attribute holds the actual image url. The second sample element has this attribute set to a non-existent url. On page load all deferred img elements are parsed. The script tries to load the actual image file in a dummy element. If it succeeds, transfers the value to the actual image tag.

Hope it helps you.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900