Created
January 19, 2019 07:55
-
-
Save rodrigopolo/0de2c44c36accfa1561f530993d6ceab to your computer and use it in GitHub Desktop.
Simple and more elaborated Guatemalan CUI validation
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
// Simple | |
function valCUI(cui){ | |
var nd, v, t=0, dl, dn = [17,8,16,16,13,14,19,8,24,20,9,29,32,22,8,17,12,5,10,11,7,17]; | |
if(nd = /^(\d{8})(\d{1})(\d{2})(\d{2})$/.exec(cui.replace(/\D/g,''))){ | |
v = parseInt(nd[2]); | |
for (var i = 0; i < nd[1].length; i++){ | |
t += nd[1][i] * (i + 2); | |
} | |
if((t % 11) === v){ | |
if(dl=dn[nd[3]-1]){ | |
return (nd[4]>=1 && nd[4]<=dl); | |
}else{ | |
return false; | |
} | |
}else{ | |
return false; | |
} | |
}else{ | |
return false; | |
} | |
} | |
// Elaborated: | |
var gd = ['Guatemala','El Progreso','Sacatepequez','Chimaltenango','Escuintla','Santa Rosa','Sololá','Totonicapán','Quetzaltenango','Suchitepequez','Retalhuleu','San Marcos','Huehuetenango','El Quiché','Baja Verapaz','Alta Verapaz','El Petén','Izabal','Zacapa','Chiquimula','Jalapa','Jutiapa']; | |
var gm = [ | |
['Guatemala', 'Sta. Catarina Pinula', 'San José Pinula', 'San José del Golfo', 'Palencia', 'Chinautla', 'San Pedro Ayampuc', 'Mixco', 'san Pedro Sacatepequez', 'San Juan Sacatepequez', 'San Raymundo', 'Churrancho', 'Fraijanes', 'Amatitlan', 'Villa Nueva', 'Villa Canales', 'San Miguel Petapa'], | |
['Guastatoya','Morazán','San Agustin Acasaguastlan','San Cristobal Acasaguastlan','El Jicaro','Sansare','Sanarate','San Antonio La Paz'], | |
['Antigua','Jocotenango','Pastores','Sumpango','Santo Domingo Xenacoj','Santiago Sacatepequez','San Bartolomé Milpas Altas','San Lucas Sacatepequez','Santa Lucía Milpas Altas','Magdalena Milpas Altas','Santa María de Jesús','Ciudad Vieja','San Miguel Dueñas','Alotenango','San Antonio Aguas Calientes','Santa Catarina Barahona'], | |
['Chimaltenango','San José Poaquíl','San Martín Jilotepeque','San Juan Comalapa','Santa Apolonia','Tecpan Guatemala','Patzun','San Miguel Pochuta','Patzicía','Santa Cruz Balanya','Acatenango','San Pedro Yepocapa','San Andres Itzapa','Parramos','Zaragoza','El Tejar'], | |
['Escuintla','Santa Lucía Cotzumalguapa','La Democracia','Siquinalá','Masagua','Tiquistate','La Gomera','Guanagazapa','San José','Itzapa','Palín','San Vicente Pacaya','Nueva Concepción'], | |
['Cuilapa','Barberena','Santa Rosa de Lima','Casillas','San Rafaél Las Flores','Oratorio','San Juan Tecuaco','Chiquimulilla','Taxisco','Santa María Ixhuatan','Guazacapán','Santa Cruz Naranjo','Pueblo Nuevas Viñas','Nueva Santa Rosa'], | |
['Sololá','San José Chacaya','Santa María Visitación','Santa María Utatlán','Nahualá','Santa Catarina Ixtahuacán','Santa Clara La Laguna','Concepción','San Andres Semetabaj','Panajachel','Santa Catarina Palopó','San Antonio Palopó','San Lucas Tolimán','Santa Cruz La Laguna','San Pablo La Laguna','San Marscos La Laguna','San Juán La Laguna','San Pedro La Laguna','Santiago Atitlán'], | |
['Totonicapán','San Cristobal Totonicapán','San Francisco El Alto','San Andres Xecul','Momostenango','Santa María Chiquimula','Santa Lucía La Reforma','San Bartolo Aguas Calientes'], | |
['Quetzaltenango','Salcajá','Olintepeque','San Carlos Sija','Sibilia','Cabrican','Cajola','San Miguel Siguila','San Juan Ostuncalco','San Mateo','Concepción Chiquirichapa','San Martín Sacatepequez','Almolonga','Cantel','Huitán','Zunil','Colomba Costa Cuca','San Francisco La Unión','El Palmar','Coatepeque','Genova Costa Cuca','Flores Costa Cuca','La Esperanza','Palestina de Los Altos'], | |
['Mazatenango','Cuyotenango','San Francisco Zapotitlán','San Bernardino','San José El Ídolo','Santo Domingo Suchitepequez','San Lorenzo','Samayac','San Pablo Jocopilas','San Antonio Suchitepequez','San Miguel Panam','San Gabriel','Chicacao','Patulul','Santa Bárbara','San Juán Bautista','Santo Tomas La Unión','Zunilito','Pueblo Nuevo','Río Bravo'], | |
['Retalhuleu','San Sebastían','Santa Cruz Mulua','San Martín Zapotitlán','San Felipe','San Andres Villa Seca','Champerico','Nuevo San Carlos','El Asintal'], | |
['San Marcos','San Pedro Sacatepequez','San Antonio Sacatepequez','Comitancillo','San Miguel Ixtahuacán','Concepción Tutuapa','Tacaná','Sabinal','Tajumulco','Tejutla','San Rafaél Pie de la Cuesta','Nuevo Progreso','El Tumbador','San José El Rodeo','Malacatán','Catarina','Ayutla Tecún Uman','Ocos','san Pablo','El Quetzal','La Reforma','Pajapita','Ixchiguan','San José Ojetenan','San Cristobal Chucho','Sipacapa','Esquipulas Palo Gordo','Río Blanco','San Lorenzo'], | |
['Huehuetenango','Chiantla','Malacatancito','Cuilco','Nenton','San Pedro Necta','Jacaltenango','San Pedro Soloma','San Ildefonso Ixtahuacán','Santa Bárbara','La Libertad','La Democracia','San Miguel Acatán','San Rafaél La Independencia','Todos Santos Cuchumatán','San Juán Atitán','Santa Eulalia','San Mateo Ixtatán','Colotenango','San Sebastián Huehuetenango','Tectitán','Concepción Huista','San Juán Ixcoy','San Antonio Huista','San Sebastián Coatán','Santa Cruz Barillas ','Aguacatán','San Rafaél Petzal','San Gaspar Ixchil','Santiago Chimaltenango','Santa Ana Huista','Unión Cantinil'], | |
['Santa Cruz del Quiché','Chiché','Chinique','Zacualpa','Chajul','Sto. Tomás Chichicastenango','Patzite','San Antonio Ilotenango','San Pedro Jocopilas','Cunen','San Juán Cotzal','Joyabaj','Nebaj','San Andrés Sajcabaja','San Miguel Uspantan','Sacapulas','San Bartolomé Jocotenango','Canilla','Chicaman','Ixcán','Pachalun','Playa Grande'], | |
['Salamá','San Miguel Chicaj','Rabinal','Cabulco','Granados','Santa Cruz el Chol','San Jerónimo','Perulha'], | |
['Cobán','Santa Cruz Verapaz','San Cristobal Verapaz','Tactic','Tamahu','San Miguel Tucurú','Panzos','Senahu','San Pedro Carcha','San Juan Chamelco','Lanquín','Santa María Cahabon','Chisec','Chahal','Fray Bartolomé de Las Casas','La Tinta','Raxruha'], | |
['Flores Costa Cuca','San José','San Benito','San Andrés Sajcabaja','La Libertad','San Francisco','Santa Ana Huista','Dolores','San Luis','Sayaxche','Melchor de Mencos','Poptún'], | |
['Puerto Barrios','Livingston','El Estor','Morales','Los Amates'], | |
['Zacapa','Estanzuela','Río Hondo','Gualán','Teculután','Usumatlán','Cabañas','San Diego','La Unión','Huité'], | |
['Chiquimula','San José La Arada','San Juán La Ermita','Jocotán','Camotán','Olopa','Esquipulas','Concepción Las Minas','Quetzaltepeque','San Jacinto','Ipala'], | |
['Jalapa','San Pedro Pinula','San Luis Jilotepeque','San Manuel Chaparrón','San Carlos Alzatate','Monjas','Mataquescuintla'], | |
['Jutiapa','El Progreso','Santa Catarina Mita','Agua Blanca','Asunción Mita','Yupiltepeque','Atescatempa','Jeréz','El Adelanto','Zapotitlán','Compa','Jalpatagua','Coguaco','Moyuta','Pasaco','San José Acatempa','Quesada'], | |
]; | |
function valCUIDM(cui){ | |
var nd,v,t=0,rd,rm; | |
if(nd = /^(\d{8})(\d{1})(\d{2})(\d{2})$/.exec(cui.replace(/\D/g,''))){ | |
v = parseInt(nd[2]) | |
for (var i = 0; i < nd[1].length; i++){ | |
t += nd[1][i] * (i + 2); | |
} | |
if((t % 11) === v){ | |
if(rd=gd[nd[3]-1]){ | |
if(rm=gm[nd[3]-1][nd[4]-1]){ | |
return { | |
dep:rd, | |
mun:rm | |
}; | |
}else{ | |
return false; | |
} | |
}else{ | |
return false; | |
} | |
}else{ | |
return false; | |
} | |
}else{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment