caolan / caolan/forms

Use WHATWG URL for url and email validation

Open
#203 10 comments 0 reactions 0 assignees View on GitHub
Feature Requests
Dominant language
JavaScript
Stars
1k
Forks
161
PR merge metrics
No merged PRs in 30d

Description

Via [universal-url](https://npmjs.com/universal-url) as it covers far more edge cases such as IDNAs and IPv6 than a simple regex will.

```js
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;
}
};
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.