Verify an image URL in JavaScript
📅July 30, 2021
In this short article, we will show you how to verify an image URL in JavaScript.
Here, we will use the Regular Expression">Regular Expression to validate the image url using JavaScript.
Checkout more articles on JavaScript
- Navigator object in JavaScript
- math-functions-in-javascript" title="Math functions in JavaScript">Math functions in JavaScript
- Methods of Promises in JavaScript
- 7 JavaScript One-Liners that you should know
- console methods in JavaScript
Use the following function to check the extension using a regular expression.
Function
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
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..!! 🙂