Skip to content

manuelmhtr/validate-rfc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Validate RFC

A simple and lightweight library to validate Mexican RFCs (Tax ID).

Install

NodeJS

Use NPM:

$ npm install --save validate-rfc

Or YARN:

$ yarn add validate-rfc

Browser

Add the script to your project:

<!-- Latest version -->
<script src="https://cdn.jsdelivr.net/gh/manuelmhtr/validate-rfc@latest/dist/index.js" type="text/javascript"></script>

<!-- Or specify a version -->
<script src="https://cdn.jsdelivr.net/gh/manuelmhtr/validate-rfc@v2.0.2/dist/index.js" type="text/javascript"></script>

<!-- This will export a global function "validateRfc": -->
<script type="text/javascript">
  var data = validateRfc('mhtr93041179a');
  console.log(data);
</script>

API

The library only exposes a single function (.validateRfc).

.validateRfc(rfc)

Checks whether a string is a valid RFC and returns validation details.

Parameters

Parameter Type Description
rfc String The RFC to be validated.
options Object Settings (Optional).
options.omitVerificationDigit Boolean When true, the Check digit is omitted from the validation (Default: false). This option is useful since there are some RFCs with an invalid Check digit that are recognized as valid by the Mexican authorities (See Known issues).

Response

It returns a plain object with the values:

Parameter Type Description
isValid Boolean Indicates if the string is a valid RFC.
rfc String The formatted RFC (uppercase, with no white spaces or symbols). Returns null when input is an invalid RFC.
type String The classification of the provided RFC. Values can be person for a legal person, company for companies, generic for the generic RFC "XAXX010101000" ir foreign for the RFC "XEXX010101000" which is used by foreign people. Returns null when input is an invalid RFC.
errors Array[String] In case the RFC is invalid, the reasons why the RFC is invalid will be listed here.

Possible errors values and they description are:

Error Descripción
INVALID_FORMAT The format is invalid, that means, the string does not meet with the required length or expected structure. Eg: XYZ because clearly is not an RFC.
INVALID_DATE The string may have the correct format, but digits generate an invalid date. Eg: MHTR815511A70 because it refers to month 55.
INVALID_VERIFICATION_DIGIT The string has a valid format, but the last character (check digit) is invalid. Eg: MHTR810511A79 ends with 9 but it is expected to end with 2.
FORBIDDEN_WORD The string contains one of the inconvenient words that cannot be included in an RFC. Eg: FETO930411792 the initials make the word FETO (fetus, LOL).

Example

const validateRfc = require('validate-rfc');

const response = validateRfc('mhtr93041179a');
console.log(response);

/*
Prints:

{
  isValid: true,
  rfc: 'MHTR93041179A',
  type: 'person'
}

*/

const response = validateRfc('This is not an RFC');
console.log(response);

/*
Prints:

{
  isValid: false,
  rfc: null,
  type: null,
  errors: ['INVALID_FORMAT']
}

*/

Tests

Run the test with the command:

$ yarn test

Known issues

Check digit mismatches

Some RFCs registered by the SAT (Mexican taxing bureau) does not pass the Check digit validation. For instance, LME060822IH5 is a valid RFC and able to be invoices according the SAT official validator, however its Check digit should be 3 rather than 5. Therefore in those cases, this library returns isValid as false and the error INVALID_VERIFICATION_DIGIT.

Solutions:

  1. If you find one of these cases, add it to the list of valid RFCs. Upper-cased and sorted alphabetically. By adding them to the list you are supporting the documentation of such cases and preventing from returning an error the next time.
  2. Use the option omitVerificationDigit to ignore the Check digit validation. It has the advantage of being a faster solution and covers all the cases, but many invalid RFCs will pass the validation.

References:

Used by

Used by tens of successful teams:


Zenfi

Yotepresto

DeAcero

Related

Licencia

MIT

About

Una librería sencilla para validar RFCs mexicanos

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •