Need help with cpf?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

fnando
150 Stars 7 Forks MIT License 20 Commits 11 Opened issues

Description

🇧🇷 Validate, generate and format CPF numbers

Services available

!
?

Need anything else?

Contributors list

# 27,391
sparkli...
Ruby
Node.js
Rails
16 commits
# 77,103
React N...
Ruby
Objecti...
ssl-pin...
1 commit
# 502,006
TypeScr...
npm
cpf
tests
1 commit

CPF

Build Status NPM package version License: MIT Minified size Minified+Gzip size

This package does some CPF magic. It allows you to create, validate and format CPF documents.

HINT: Check out the CNPJ counter part available at https://github.com/fnando/cnpj.

Installation

This lib is available as a NPM package. To install it, use the following command:

npm install @fnando/cpf --save

If you're using Yarn (and you should):

yarn add @fnando/cpf

Usage

// Node.js-specific
const cpf = require("@fnando/cpf/commonjs");

// @import import * as cpf from "@fnando/cpf"; // import the whole library import { isValid as isValidCpf } from "@fnando/cpf"; // import just one function

// import via

cpf.isValid("532.820.857-96"); //=> true

cpf.isValid("53282085796"); //=> true

cpf.strip("532.820.857-96"); //=> 53282085796

cpf.format("53282085796"); //=> 532.820.857-96

cpf.generate(true); // generate formatted number //=> 838.684.734-40

cpf.generate(); // generate unformatted number //=> 72777632898

On the web, without transformation, just use

web/cpf.min.js
.

Strict Validation

By default, validations will strip any characters you provide. This means that the following is valid, because only numbers will be considered:

cpf.isValid("101#688!!!!!!542......36");
//=> true

cpf.strip("101#688!!!!!!542......36"); //=> 10168854236

If you want to strict validate strings, use the following signature:

cpf.isValid(number, strict);

The same example would now return

false
:
cpf.isValid("101#688!!!!!!542......36", true);
//=> false

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.