Agegate
A simple function that verifies a date of birth against a country's legal drinking age.
npm i agegate
Usage
; var user = dateOfBirth: "2015-02-14" // strings are also accepted country: "US"; var isLegal = ; // false
⚠️ 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.
;; const countries = ; { const date setDate = ; const country setCountry = ; const legal setLegal = ; const submitHandler = { e; if date && country const result = ; ; }; return <div> <form onSubmit=submitHandler> <h3>Enter your date of birth</h3> <input type="date" value=date onChange= /> <h3>Enter your country</h3> <select value=country onChange= > countries </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.