Validation
The library provides three ways to validate NIC numbers, each suited for different use cases.
NIC.validate()
Throws a NICError if the NIC is invalid. Use this when you want to halt execution on bad input:
import { NIC } from "@sri-lanka/nic";
NIC.validate("901404567V"); // passes silently
NIC.validate("invalid"); // throws NICError: "Invalid NIC structure"NIC.valid()
Returns true or false without throwing. Perfect for form validation:
NIC.valid("901404567V"); // true
NIC.valid("invalid"); // falseTip:
NIC.valid()only catchesNICError. If your customcheckfunction throws a different error type (likeTypeError), it will still propagate.
NIC.parse()
Parsing also validates. If the NIC is invalid, .parse() throws before returning any data:
try {
const result = NIC.parse("invalid");
} catch (error) {
// NICError: "Invalid NIC structure"
}Validation Pipeline Order
When you parse or validate a NIC, the library executes checks in a very specific order:
- Sanitization: Trims whitespace and converts trailing letters to uppercase.
- Structural Validation: Tests the string against strict Regular Expressions to ensure it is exactly 12 digits or 9 digits plus V/X.
- Extraction: Calculates the
yearanddaysfrom the raw string. - Logic Validations: Verifies that the days correspond to a real day in that specific birth year, and that the computed age falls within the permitted minimum/maximum limits.
- Type Restrictions: If
onlyNeworonlyOldis set, rejects NICs of the wrong format. - Custom Validation (
checkcallback): Finally, if you provided acheckfunction in the config, it executes your custom rules. This guarantees your custom function only runs if the NIC is structurally and mathematically sound.
What Gets Validated
The library checks these things automatically:
- Structure — Must match either 12-digit or 9-digit+letter pattern
- Day of year — Must be between 1 and 365 (or 366 for leap years)
- Minimum age — Default: 15 (legal age to hold a NIC)
- Maximum age — Default: current year minus 1900
- Birth year range — Must fall within the valid range for the NIC format
- Type restrictions — If
onlyNeworonlyOldis set, the NIC must be the specified format
What is NOT Validated
Because this library runs purely offline mathematical checks, it does not:
- Check against government databases — It cannot verify if a NIC has actually been issued to a real person.
- Verify the serial number or letter — The serial number indicates the sequence of issue, and the trailing letter (V/X) indicates voter status. These cannot be validated offline.
- Verify the check digit — The exact modulo algorithm used by the Department for Registration of Persons to calculate the final digit is not public. The library ensures it is a number, but cannot verify if it is mathematically correct.
Error Handling
All validation errors are instances of NICError:
import { NIC, NICError } from "@sri-lanka/nic";
try {
NIC.parse("invalid");
} catch (error) {
if (error instanceof NICError) {
console.log(error.message); // "Invalid NIC structure"
console.log(error.name); // "NICError"
}
}See the Errors page for a full list of error messages.