question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validate localhost url

See original GitHub issue

Could you please provide a way to allow localhost as a valid url string? Something like validate.js’s allowLocal flag: https://validatejs.org/#validators-url:

let validationSchema = Yup.object().shape({
    url: Yup.string().url('Please enter a valid URL, including scheme', { allowLocal: true })
});

Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:16
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
diogommartinscommented, May 31, 2019

I don’t have time right this second to make and test the modification and submit a PR, though I might in the next few days. In case I don’t get to it, here’s a regex you can use to replace the one rUrl one you’re already using in string.js:

^(?:([a-z0-9+.-]+):\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*\.?)(?::\d{2,5})?(?:[/?#]\S*)?$

I tested it here: https://www.regexpal.com/?fam=104762

It’s derived from https://gist.github.com/dperini/729294 with a few modifications:

  • Scheme restriction removed. Any RFC valid scheme is OK now, not just http(s) and ftp.
  • Private IP restriction removed. (i.e. 192.168.1.2… is OK now)
  • TLD requirement removed. (‘localhost’ and other single-word hostnames OK now)

dperini’s regex is pretty widely regarded as the best one for matching URLs. The alternative is to use a real URL parsing lib instead of a regex, which is probably a good idea, but will increase the size of this lib.

@jquense I’m having the same problem trying to use yup.string().url() to validate an URL starting with “http://localhost:3000/”. I’ve seen that this issue was closed but I’m interested in fixing the problem. Would you still accept a PR for a regex that supports that kind of URL?

5reactions
edqallencommented, Aug 30, 2018

I don’t have time right this second to make and test the modification and submit a PR, though I might in the next few days. In case I don’t get to it, here’s a regex you can use to replace the one rUrl one you’re already using in string.js:

^(?:([a-z0-9+.-]+):\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*\.?)(?::\d{2,5})?(?:[/?#]\S*)?$

I tested it here: https://www.regexpal.com/?fam=104762

It’s derived from https://gist.github.com/dperini/729294 with a few modifications:

  • Scheme restriction removed. Any RFC valid scheme is OK now, not just http(s) and ftp.
  • Private IP restriction removed. (i.e. 192.168.1.2… is OK now)
  • TLD requirement removed. (‘localhost’ and other single-word hostnames OK now)

dperini’s regex is pretty widely regarded as the best one for matching URLs. The alternative is to use a real URL parsing lib instead of a regex, which is probably a good idea, but will increase the size of this lib.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UrlValidator is not working for localhost - Stack Overflow
I'm facing an issue that i'm using UrlValidator in my code. UrlValidator urlValidator = UrlValidator.getInstance(); if(urlValidator.isValid(url)) ...
Read more >
Check if a URL is localhost in JavaScript | bobbyhadz
Use the includes() method to check if a URL is localhost . The includes method will return true if the URL is localhost,...
Read more >
URL (with localhost) - Regex Tester/Debugger
Test String. http://localhost ... Url Validation Regex | Regular Expression - Taha · date format (yyyy-mm-dd) · Match an email address
Read more >
3421 (URLField does not validate IP addresses, or localhost)
In [47]: f.clean('http://localhost') ... ValidationError: [u'Enter a valid URL.'] In [48]: f.clean('http:// ...
Read more >
Check if an URL is valid or not using Regular Expression
The URL must start with either http or https and · then it must contain www. and · then followed by subdomain of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found