// Function to validate a URL using a regular expression
function isValidURL(url) {
const urlRegex = /^(ftp|http|https):\/\/[a-zA-Z0-9-]+(\.[a-zA-Z]{2,})+(\/[^\s]*)?$/;
return urlRegex.test(url);
}
// Example URL to validate
const websiteURL = 'https://www.example.com';
// Check if the URL is valid using the isValidURL function
console.log('Is Valid URL:', isValidURL(websiteURL));JavaScript Validate URL
javascript
Related Posts
More content you might like
Tutorial
Different Ways to Validate URLs in JavaScript
type="url": This input type forces the browser to validate the input as a URL.- Native Browser Validation: The browser provides validation feedback, preventing the form from submitting if the URL is invalid.
Limitations:
Aug 29, 2024
Read More Code
javascript
Checking if the User is Accessing from a Mobile Device in JavaScript
No preview available for this content.
Jan 26, 2024
Read More Code
javascript
Validating Credit Card Numbers in JavaScript
No preview available for this content.
Jan 26, 2024
Read More Code
javascript
Detecting Browser and Version
No preview available for this content.
Jan 26, 2024
Read MoreDiscussion 0
Please sign in to join the discussion.
No comments yet. Be the first to share your thoughts!