Question: How can I verify that the format of a Mexican CURP is valid?
What is CURP? The Unique Population Registry Key (CURP) is a unique 18-character alphanumeric identity code used to officially identify both Mexican residents and citizens, issued by RENAPO .
It is formed from the letters of the names and surnames, the date and the state of birth, and the sex. Also, character 17 is to avoid duplicates, and the last character is a digit that is used as an error detector-corrector. The syntax is detailed in the Normative Instructions for the Assignment of the Unique Population Registry Key ? .
Context: I'm only interested in validating that a key could be valid. I am not interested at this point to see if that CURP exists or not.
So far, I'm just checking that it's 18 alphanumeric characters with the regex:
/^[A-Z\d]{18}$/
but I am also interested in considering that the characters are valid, and that the check digit matches.
Regular phrase
The following regular expression checks:
Full validation
I publish the code in JavaScript to be able to run it here, but I'm sure it's very easy to port to any other language.
Description
Taking into account the structure with which a CURP is formed:
[A-Z]
(if it is another letter, a
X
) is used.[AEIOUX]
(or one
X
if it does not have one).[A-Z]{2}
\d{2}
year.(?:0[1-9]|1[0-2])
month.(?:0[1-9]|[12]\d|3[01])
day.[HM]
(?:AS|B[CS]|C[CLMSH]|D[FG]|G[TR]|HG|JC|M[CNS]|N[ETL]|OC|PL|Q[TR]|S[PLR]|T[CSL]|VZ|YN|ZS)
[B-DF-HJ-NP-TV-Z]{3}
[A-Z\d]
(\d)
After seeing if it matches the regex, we check that the check digit is valid. That is, if it matches the calculated first 17 characters:
To calculate the digit, first add the value of each of the 17 characters, which have a value from 0 to 36 according to this order (dictionary):
And take the 10's complement of the last digit of this sum (or
0
if it gives 10).My answer is a bit late, but some time ago I made this method to validate RFCs, both for individuals and legal entities. I took the regex from the official SAT site to validate RFC. I hope someone finds it useful. The function is done in
JS
Thank you very much. This helped me create a validator for PHP, I leave the code in case it helps someone.
A REST API to validate CURP and get basic information about the person. The API is Free (unlimited requests), Fast (200 ms latency on average) and Standardized according to ISO. More information using Postman
Request example:
Successful response example:
Documentation
Here I leave you a way to execute it in PHP so that you create a function that returns true or false completely roasted in the algorithm that @Mariano left
It is very important that you use the
mb_
string functions since it gave me a headache since I did not remember that the Ñ takes two characters internally, the string functions that do not use mb_ at the beginning use positions per byte, not per characterYou can validate a CURP and the person's information using this API https://rapidapi.com/acrogenesis/api/curp-renapo .
For example to use it in php it would be like this:
And it would return the information to you like this:
You can use the curp library that is in NPM
If you don't use npm add it with bundle like this.
Functional example:
One more alternative, without using javascript, validating only the format with the property
pattern
, has the detail that we cannot check the check digit.In python you can use the CURPSuite library , which performs CURP format validation.
To install it:
Example:
After creating a CURP object, you can also get its properties if desired.
Documentation.
Disclaimer: I am the developer of CURPSuite.
According to a document issued by the SAT , they mention on page 3 the following REGEX pattern, which does not validate the content , but only the format of the text string :
However, it is incorrect in the penultimate part
[0-9,AZ]
, which should be like this[0-9,A-Z]
At the code level (Java), its validation could be something like this in a Utility class: