Verify an image URL in JavaScript
In this short article, we will show you how to verify an image URL in JavaScript.
Here, we will use the Regular Expression to validate the image url using JavaScript.
Checkout more articles on JavaScript
Use the following function to check the extension using a regular expression.
Function
1 2 3 4 | function checkURL(url) { if (typeof url !== 'string') return false; return (url.match(/\.(jpg|jpeg|gif|png)$/) != null); } |
Here we have validated the extension such as jpg
, jpeg
, gif
, and png
. This function will return the boolean value.
Output
1 2 3 | checkURL('cluemediator.com'); // Output: false checkURL('https://xyz.com/abc.png'); // Output: true checkURL('http://abc.com/xyz.gif'); // Output: true |
In the upcoming article, we will show you how to check if an image exists from a URL.
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂