Validate, standardise and classify NHS and Scottish CHI numbers in JavaScript and TypeScript. The library is in early development.
Install from npm:
npm install @ohtf/nhs-number-js
node --input-type=module -e "import { isValid } from '@ohtf/nhs-number-js'; console.log(isValid('987 654 3210', { forRegion: 'SYNTHETIC' }));"
# trueOne TypeScript source builds to an ES module (.js) and declarations (.d.ts). The same import works in JavaScript and TypeScript, including browser apps using a bundler. On Node.js 22.13 or newer, JavaScript callers can also use require('@ohtf/nhs-number-js').
Use nhs-number-js if you need the same CHI and range checks as OHTF’s Python library. In a side-by-side check of the versions below, all three alternatives accepted a CHI number with an impossible date and a matching Modulus 11 digit, and rejected a valid Modulus 10-only CHI number. This library handles both cases, lets you opt out of Modulus 10, and can report or require a region by numeric range. Those behaviours are checked against the shared Python vectors.
| Alternative tested | Where it may fit better |
|---|---|
validate-nhs-number 1.1.1 |
Accepts digit arrays and whitespace outside the usual 3-3-4 grouping, while rejecting punctuation. Can be loaded as a classic browser script. |
valid-nhsnum 1.0.4 |
Includes a number generator and accepts more free-form pasted input. It also accepts mixed separators and punctuation that this library deliberately rejects. |
nhs-number-validator 1.1.2 |
Includes an interactive CLI and can be loaded as a classic browser script. It expects an unformatted number. |
All four agreed on the plain ten-digit Modulus 11 examples tested, with a correct and an incorrect check digit. The comparison used published test examples, not patient records. It did not measure speed or bundle size.
| Export | Result |
|---|---|
standardiseFormat(input) |
Ten ASCII digits or '' for unparseable input. Accepts ten-digit strings, consistent 3-3-4 groups, and non-negative integers of up to ten digits. Prefer strings to preserve leading zeroes. |
isValid(input, options?) |
Checks Modulus 11, plus the encoded calendar date for Scottish CHI numbers. Also accepts Modulus 10 for CHI unless checkMod10: false. Set forRegion to require a specific region. |
regionOf(input) |
A region name or null. Classifies by numeric range without validating the checksum or date. |
calculateChecksum(identifier) / calculateChecksumMod10(identifier) |
Check digit calculated from a nine-digit string, or null for invalid input. A Modulus 11 result of 10 cannot be used as a check digit. |
RANGES |
Read-only inclusive range boundaries. |
TypeScript also exports Region and ValidationOptions. Region names are SCOTLAND, NORTHERN_IRELAND, ENGLAND_WALES_IOM, EIRE, RESERVED, UNALLOCATED and SYNTHETIC.
Validation does not verify patient identity or confirm that a number was issued. It can accept reserved and synthetic numbers. See safety notes before patient-facing use. Number generation and disguising are outside this library’s scope.
Run s/test after npm ci --ignore-scripts. The tests use the Python library’s unmodified JSON vectors and exercise the JavaScript entry point and TypeScript declarations. See the validation contract, contribution guide and npm release procedure. The library has no runtime dependencies. TypeScript 6 is the sole dev dependency: it emits JavaScript and declarations without a bundler or platform-specific compiler packages.
Code is MIT. Project documentation is CC BY-SA 4.0. The MIT-licensed fixture and reference behaviour come from OHTF’s Python nhs-number; the upstream copyright notice is retained.