diff --git a/src/lib/isURL.js b/src/lib/isURL.js index 8ae971ed6..7c20cbffe 100644 --- a/src/lib/isURL.js +++ b/src/lib/isURL.js @@ -113,8 +113,14 @@ export default function isURL(url, options) { // 2. There's an `@` symbol before any `/` // 3. The part before `@` contains only valid auth characters (alphanumeric, -, _, ., %, :) const starts_with_slashes = after_colon.slice(0, 2) === '//'; + const is_port = /^[0-9]+(?:\/|$)/.test(after_colon); - if (!starts_with_slashes) { + if (!starts_with_slashes && is_port) { + // This is a hostname with a port, not a protocol (e.g., localhost:3000) + if (options.require_protocol) { + return false; + } + } else if (!starts_with_slashes) { const first_slash_position = after_colon.indexOf('/'); const before_slash = first_slash_position === -1 ? after_colon diff --git a/test/validators.test.js b/test/validators.test.js index 5196c6fe9..9db1f1745 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -499,6 +499,7 @@ describe('Validators', () => { 'localhost', 'localhost:3000', 'service-name:8080', + 'my-3272-service:9000', 'https://localhost', 'http://localhost:3000', 'http://service-name:8080',