Created
June 11, 2020 20:13
-
-
Save proteo5/0a493e1c60114531f6ae42040750d62c to your computer and use it in GitHub Desktop.
Métodos en C# para Validar el CURP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private bool CurpValida(string curp) | |
{ | |
var re = @"^([A-Z][AEIOUX][A-Z]{2}\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])[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)$"; | |
Regex rx = new Regex(re, RegexOptions.Compiled | RegexOptions.IgnoreCase); | |
var validado = rx.IsMatch(curp); | |
if (!validado) //Coincide con el formato general? | |
return false; | |
//Validar que coincida el dígito verificador | |
if (!curp.EndsWith(DigitoVerificador(curp.ToUpper()))) | |
return false; | |
return true; //Validado | |
} | |
private string DigitoVerificador(string curp17) | |
{ | |
//Fuente https://consultas.curp.gob.mx/CurpSP/ | |
var diccionario = "0123456789ABCDEFGHIJKLMNÑOPQRSTUVWXYZ"; | |
var suma = 0.0; | |
var digito = 0.0; | |
for (var i = 0; i < 17; i++) | |
suma = suma + diccionario.IndexOf(curp17[i]) * (18 - i); | |
digito = 10 - suma % 10; | |
if (digito == 10) return "0"; | |
return digito.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Como sugerencia para evitar múltiples return podrías regresar el resultado de la evaluación entre validado && EndsWith DigitoVerificador, gracias al "short-circuit evaluation" si validado no se cumple no se ejecuta la segunda parte.