https://github.com/izolate/agegate
A simple function that verifies a date of birth against a country's legal drinking age.
https://github.com/izolate/agegate
age-gate countries country-data
Last synced: 7 months ago
JSON representation
A simple function that verifies a date of birth against a country's legal drinking age.
- Host: GitHub
- URL: https://github.com/izolate/agegate
- Owner: izolate
- License: mit
- Created: 2015-04-14T16:03:00.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-03-27T19:28:26.000Z (over 3 years ago)
- Last Synced: 2025-03-17T18:21:47.830Z (7 months ago)
- Topics: age-gate, countries, country-data
- Language: JavaScript
- Homepage: https://izolate.github.io/agegate/
- Size: 420 KB
- Stars: 16
- Watchers: 2
- Forks: 5
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Agegate
A simple function that verifies a date of birth against a country's legal drinking age.
```
npm i agegate
```## Usage
```js
import agegate from "agegate";var user = {
dateOfBirth: new Date("2015-02-14"), // strings are also accepted
country: "US",
};var isLegal = agegate(user.dateOfBirth, user.country); // false
```:warning: If an invalid date is supplied, the result will be falsy. If an invalid country code is supplied, it will validate against a default legal drinking age of 18.
### Use with frameworks (e.g. React)
In order to use this library with frontend UI frameworks, the underlying dataset used to validate is also exported.
```js
import React, { useState } from "react";
import agegate, { getData } from "agegate";const countries = getData();
function Modal() {
const [date, setDate] = useState("");
const [country, setCountry] = useState(countries[0].code);
const [legal, setLegal] = useState(false);const submitHandler = e => {
e.preventDefault();if (date && country) {
const result = agegate(new Date(date), country);
setLegal(result);
}
};return (
Enter your date of birth
setDate(e.target.value)}
/>
Enter your country
setCountry(e.target.value)}>
{countries.map(({ code, name }) => (
{name}
))}
Submit
RESULT: You are {legal ? "" : "NOT"} old enough!
);
}
```* * *
Please file a [new issue](https://github.com/izolate/agegate/issues/new) if you find any inconsistencies in the countries dataset.