In React Native, how do I check if a given URL is a valid image?
For example, if the url is https://stackoverflow.com/, it should return false. And if it is https://i.imgur.com/qMUWuXV.jpg, it should return true
In React Native, how do I check if a given URL is a valid image?
For example, if the url is https://stackoverflow.com/, it should return false. And if it is https://i.imgur.com/qMUWuXV.jpg, it should return true
Try this
checkValidUrl = (url) => {
//define some image formats
var types = ['jpg','jpeg','tiff','png','gif','bmp'];
//split the url into parts that has dots before them
var parts = url.split('.');
//get the last part
var extension = parts[parts.length-1];
//check if the extension matches list
if(types.indexOf(extension) !== -1) {
return true;
}
}
console.log(checkValidUrl("https://homepages.cae.wisc.edu/~ece533/images/airplane.png"));
Try this:
checkValidURLImage(){
var a = "https://i.imgur.com/qMUWuXV.jpg"; //your url
var b = ["jpeg","jpg","png","gif","raw"]; //format img
var c = a.split("."); // ["https://i", "imgur", "com/qMUWuXV", "jpg"]
console.log(b.includes(c[c.length-1]))
}