idnumbers
    Preparing search index...

    Function validateNationalId

    • Validate a national ID number for a specific country.

      Delegates to the registry-based validator lookup. Aliases (e.g. "FR", "fr") are resolved to their primary alpha-3 key (e.g. "FRA") which is returned as the countryCode in the result. For a built-in country code, extractedInfo has that country's parse result type (#123). A country code that isn't a registered string, including a non-string from untyped JavaScript, yields unsupported_country and echoes the code as passed. When a validator or parser throws, the result is validation_failed with the error message, and countryCode is the resolved alpha-3 key (#301).

      Type Parameters

      • C extends string

      Parameters

      • countryCode: C
      • idNumber: string

      Returns ValidationResult<ParsedInfoFor<C>>

      import { validateNationalId } from 'idnumbers';

      const result = validateNationalId('TWN', 'A123456789');
      result.isValid; // true
      result.extractedInfo?.location; // 'A'

      // Aliases resolve to the alpha-3 code; a failure carries a machine-readable reason.
      const invalid = validateNationalId('TW', 'A12345');
      invalid.countryCode; // 'TWN'
      invalid.reason; // 'invalid_length'