CNS (SUS card)

Cartão Nacional de Saúde, the SUS identifier of a user, health professional or health facility.

  • Parity matrix

Validate

Validates a CNS number: 15 digits.

  • Definitive cards start with 1 or 2, provisional ones with 7, 8 or 9. Each kind has its own modulus 11 rule.
  • Definitive card (starts with 1 or 2): the first 11 digits are the base, then a 3-digit suffix (000 or 001), then the check digit. The check digit is 11 minus the remainder of the base's weighted sum by 11 (weights 15 down to 5), with 11 read as 0. When that result is 10, the sum is raised by 2, the digit is recomputed and the suffix is 001 instead of 000.
  • Provisional card (starts with 7, 8 or 9): the weighted sum of all 15 digits (weights 15 down to 1) must be a multiple of 11.
  • Rejects a number that starts with 5, following ANVISA.
  • Accepts the bare digits or the printed 3-4-4-4 groups split by whitespace, ., - or /. Any run of those characters is accepted between two groups, and whitespace around the value is ignored; anything else (a letter, a separator inside a group or at the ends) is rejected.
  • A number is read only when it is a safe non-negative integer. A negative, fractional, non-finite or unsafe number is invalid.
  • No official source publishes the check digit rule as a norm. The rule follows the DATASUS "Rotina de validação de CNS e Número Provisório", published on the old Cartão Nacional de Saúde site (archived copy linked), and the ANVISA page. Neither names a first digit other than 1, 2, 7, 8 or 9, so a number starting with 5 is rejected even when its weighted sum checks out.
  • Docs examples: 100000000060018 (definitive, raw check digit 10, suffix 001) and 700000000000005 (provisional) are valid; 123456789010001 (wrong check digit) and 12345678901 (wrong length) are not.
ParameterTypeRequired
valuestring | numberyes
returnsboolean

Check if a CNS (Cartão Nacional de Saúde) number is valid, the SUS (Sistema Único de Saúde) identifier of a user, health professional or health facility. The value must be the 15 digits, optionally split into the printed groups of 3-4-4-4 by whitespace, ., - or /.

  • Definitive cards start with 1 or 2, provisional ones with 7, 8 or 9; each has its own modulus 11 rule.
  • A number starting with 5 is rejected. The DATASUS validation routines (Wayback Machine copy of the file the cartaonet.datasus.gov.br site published) cover only the numbers that start with 1 or 2 (definitive) and with 7, 8 or 9 (provisional), as ANVISA does; no official document names the prefix 5, which the e-SUS APS page accepts.
import { isValidCns } from '@brazilian-utils/brazilian-utils';

isValidCns('123456789010000'); // true (definitive)
isValidCns('100000000060018'); // true (definitive, raw check digit 10, suffix 001)
isValidCns('700000000000005'); // true (provisional)
isValidCns('123.4567-8901/0000'); // true (any of the mask characters)
isValidCns(-123456789010000); // false (not a non-negative safe integer)
isValidCns('123456789010001'); // false (wrong check digit)
isValidCns('12345678901'); // false (wrong length)
isValidCns('abc123456789010000'); // false (not written as a CNS)

Source: DATASUS validation routines (Wayback Machine copy), ANVISA CNS validation page and the e-SUS APS page.

Code: brazilian-utils/javascript
Try it with JavaScript isValidCns
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (37) and the result in each library cns.isValid

Format

Formats a CNS number into groups of 3-4-4-4 digits separated by spaces.

  • options.pad left-pads with zeros to 15 digits first.
  • A number is read only when it is a safe non-negative integer. A negative, fractional, non-finite or unsafe number returns an empty string (2.4.0 read the digits of any number, sign and decimal point dropped).
  • A value with no digits (empty, or only letters and symbols) returns an empty string, even with pad. Until 2.4.0 pad returned the full zero mask (000 0000 0000 0000) for it.
  • Digits after the 15th are dropped. Every non-digit character is ignored.
ParameterTypeRequired
valuestring | numberyes
optionsFormatCnsOptionsno
options.padbooleanno
returnsstring

Format a CNS (Cartão Nacional de Saúde) number into the common display groups of 3-4-4-4 digits separated by spaces.

  • Options (FormatCnsOptions): pad left-pads the value with zeros up to the 15 slots of the pattern before masking (default false). An empty value, or one without digits, gives '' even with pad.
import { formatCns } from '@brazilian-utils/brazilian-utils';

formatCns('123456789010000'); // '123 4567 8901 0000'
formatCns(123456789010000); // '123 4567 8901 0000'
formatCns('89010001', { pad: true }); // '000 0000 8901 0001'
Code: brazilian-utils/javascript
Try it with JavaScript formatCns
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (19) and the result in each library cns.format

Parse

Removes CNS formatting and keeps only digits, capped at 15 digits.

  • A number is read only when it is a safe non-negative integer. A negative, fractional, non-finite or unsafe number returns an empty string (2.4.0 read the digits of any number, sign and decimal point dropped).
ParameterTypeRequired
valuestring | numberyes
returnsstring

Remove CNS (Cartão Nacional de Saúde) formatting, keep only digits, and cap the result to 15 digits.

import { parseCns } from '@brazilian-utils/brazilian-utils';

parseCns('123 4567 8901 0000'); // '123456789010000'
Code: brazilian-utils/javascript
Try it with JavaScript parseCns
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (9) and the result in each library cns.parse

Official sources

See also CPF

Last updated on

On this page