1

I am getting json array which contains various name/value pairs. and for every response its different as it comes from database. for every response i get, one name/value pair represents image url info. But problem is that name is not constant for all request. i.e. is for one request it can be:

'imgurl':'http://l1.yimg.com/071153.jpg'

and for other it can be anything having different name.something like:

'imginf':'http://lre.ghamg.co.in/arecon.png'.

I am looking for a way to check whether given variable represents image url(any type of image) using javascript or jquery.i am looking for any good suggestions if they can help me with this.

Thanks for stopping by...!!

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
  • 1
    Is it good enough to see the file matches a known extension (i.e. does the value end in 'jpg', 'png', 'gif' ...), or do you need to open the file and see if it in fact looks like image content? – PaulProgrammer Apr 05 '13 at 14:50
  • 2
    Did you try writing the regex (you tagged it with regex)? – WiredPrairie Apr 05 '13 at 14:52
  • @PaulProgrammer:how about other variables ending with same extension.and number of variables are very large as they come from database. – Milind Anantwar Apr 05 '13 at 14:53
  • 1
    I'd say this is a reasonable discussion of what you're trying to do with multiple options: http://stackoverflow.com/questions/169625/regex-to-check-if-valid-url-that-ends-in-jpg-png-or-gif#169656 – WiredPrairie Apr 05 '13 at 14:53
  • Can you assume about the prefix of the property name `imginf` and `imgurl`? – nhahtdh Apr 05 '13 at 14:53
  • @WiredPrairie:ya i tried with image regex. it didn't worked though. – Milind Anantwar Apr 05 '13 at 14:54
  • @nhahtdh:as i said variable names come from db. so it can be anything.not necessary that it starts with img. – Milind Anantwar Apr 05 '13 at 14:55
  • @MilindAnantwar What's the problem of other variables ending with those extensions? They'll still be images. – Loamhoof Apr 05 '13 at 14:56
  • You said "a given variable" ... that implies you know the name of the variable. If you're looking for a way to iterate through the keys, that's a different question. – PaulProgrammer Apr 05 '13 at 14:56
  • Why does the key names differ? Normally when you get something from a database, the field names are always exactly the same. – Guffa Apr 05 '13 at 14:57
  • Should you need to iterate through the keys, here's a reference: http://stackoverflow.com/questions/684672/loop-through-javascript-object – PaulProgrammer Apr 05 '13 at 14:58
  • @guffa.my bad.it's not coming from db. the whole table from db comes as a response. so you can imagine two different table for every other scenario. – Milind Anantwar Apr 05 '13 at 15:01

1 Answers1

0

You mean something like this?

var fileName= {"imgurl": ["http://l1.yimg.com/071153.jpg"]};
var file = fileName['imgurl'][0]
var ext = file.substring(file.lastIndexOf('.') + 1);


if(ext ==="gif" || ext === "jpg" ||  ext === "png"|| ext === "BMP"){
alert('image');
}  
user1740058
  • 45
  • 1
  • 9