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.

Use WHATWG URL for url and email validation

See original GitHub issue

Via universal-url as it covers far more edge cases such as IDNAs and IPv6 than a simple regex will.

const isEmail = email => {
  try {
    const url = new URL(`mailto:${email}`);
    return url.search === '';
  } catch (error) {
    return false;
  }
};

const isURL = url => {
  try {
    url = new URL(url);
    return url.protocol === 'http' || url.protocol === 'https' || url.protocol === 'ftp';
  } catch (error) {
    return false;
  }
};

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
stevenvachoncommented, Jul 5, 2017

mailto:a@b is a valid URL as well, but I would think that a@:*&^: is not a valid email address. However: https://github.com/jsdom/whatwg-url/issues/98

universal-url is, to my knowledge, 100% TR46 compliant. universal-url-lite has two shims, one with incomplete TR46 and one that relies on the native browser implementation, which all are currently incomplete.

0reactions
stevenvachoncommented, Jul 6, 2017

That’s all I can think of at the moment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to configure URL validation in your app - TinyMCE
Necessary steps for setting URL validation within your app. Use our Link checker plugin to validate links without opening or clicking on ...
Read more >
<input type="url"> - HTML: HyperText Markup Language | MDN
The input value is automatically validated to ensure that it's either empty or a properly-formatted URL before the form can be submitted.
Read more >
4.10.5 The input element - HTML Standard - whatwg
Constraint validation: While the value of the element is neither the empty string nor a valid absolute URL, the element is suffering from...
Read more >
Which characters make a URL invalid? - Stack Overflow
When validating, you should always "think positive": ask for "what is valid", everything else is invalid. Testing against the (few) valid ...
Read more >
validator - crates.io: Rust Package Registry
Common validation functions (email, url, length, . ... This implementation uses finite automata and guarantees linear time matching on all inputs.
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