CNS (SUS card)
Cartão Nacional de Saúde, the SUS identifier of a user, health professional or health facility.
Validate
- JavaScript library
- Python library
- Go library1 case fails
- Ruby library4 cases fail
- Rust library1 case fails
- .NET library4 cases fail
- Erlang library
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 (
000or001), 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 is001instead of000. - 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) and700000000000005(provisional) are valid;123456789010001(wrong check digit) and12345678901(wrong length) are not.
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
| returns | boolean |
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/javascriptTry it with JavaScript isValidCns
Shared test cases (37) and the result in each library cns.isValid
Format
- JavaScript library
- Python library
- Go library
- Ruby library5 cases fail
- Rust library
- .NET library1 case fails
- Erlang library
Formats a CNS number into groups of 3-4-4-4 digits separated by spaces.
options.padleft-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.0padreturned the full zero mask (000 0000 0000 0000) for it. - Digits after the 15th are dropped. Every non-digit character is ignored.
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
options | FormatCnsOptions | no |
options.pad | boolean | no |
| returns | string |
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):padleft-pads the value with zeros up to the 15 slots of the pattern before masking (defaultfalse). An empty value, or one without digits, gives''even withpad.
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'Try it with JavaScript formatCns
Shared test cases (19) and the result in each library cns.format
Parse
- JavaScript library
- Python library
- Go library
- Ruby library2 cases fail
- Rust library
- .NET library
- Erlang library
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).
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
| returns | string |
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'Try it with JavaScript parseCns
Shared test cases (9) and the result in each library cns.parse
Official sources
- rni-docs.anvisa.gov.br/docs/regras_gerais/…/validacaoCNS
- web.archive.org/web/20190106003442/…/Rotina_JavaScript.doc
See also CPF
Last updated on
