Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

agegate

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

agegate

A simple function that verifies a date of birth against a country's legal drinking age.

  • 4.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Agegate

A simple function that verifies a date of birth against a country's legal drinking age.

npm i agegate

Usage

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.

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 (
    <div>
      <form onSubmit={submitHandler}>
        <h3>Enter your date of birth</h3>
        <input
          type="date"
          value={date}
          onChange={e => setDate(e.target.value)}
        />

        <h3>Enter your country</h3>
        <select value={country} onChange={e => setCountry(e.target.value)}>
          {countries.map(({ code, name }) => (
            <option key={name} value={code}>
              {name}
            </option>
          ))}
        </select>

        <button type="submit">Submit</button>
      </form>

      <p style={{ color: legal ? "green" : "red" }}>
        RESULT: You are {legal ? "" : "NOT"} old enough!
      </p>
    </div>
  );
}

Please file a new issue if you find any inconsistencies in the countries dataset.

Keywords

FAQs

Package last updated on 11 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc