merlos / cedula-panama Goto Github PK
View Code? Open in Web Editor NEWValidador javascript de la cédula de panama. A javascript validator of the Panamenian id (cedula)
Home Page: http://www.merlos.org/cedula-panama/
License: MIT License
Validador javascript de la cédula de panama. A javascript validator of the Panamenian id (cedula)
Home Page: http://www.merlos.org/cedula-panama/
License: MIT License
"""
Copyright (c) 2017 Juan M. Merlos (@merlos)
Update by Christhoval Barba (@christhoval06)
"""
from re import match
ONLY_FIRST = '^(?:PE|E|N|[23456789]|[23456789](?:A|P)?|1[0123]?|1[0123]?(?:A|P)?)$'
FIRST_AND_BOOK = '^(?:PE|E|N|[23456789]|[23456789](?:AV|PI)?|1[0123]?|1[0123]?(?:AV|PI)?)-?$'
BOOK_AND_TOME = '^(?:PE|E|N|[23456789](?:AV|PI)?|1[0123]?(?:AV|PI)?)-(?:\d{1,4})-?$'
FULL_ID = '^(PE|E|N|[23456789](?:AV|PI)?|1[0123]?(?:AV|PI)?)-(\d{1,4})-(\d{1,5})$'
FULL_VALID = '^P$|{}|{}|{}|{}'.format(ONLY_FIRST, FIRST_AND_BOOK, BOOK_AND_TOME, FULL_ID)
ONLY_VALID_ID = FULL_ID
LETTER = '^PE|E|N$'
NUMBER = '^(1[0123]?|[23456789])?$'
MIXED_ID = '^(1[0123]?|[23456789])(AV|PI)$'
def validate(id):
"""
Accepted patterns:
- Regular (provincia-libro-tomo). Ej: 1-1234-12345
- Panameño nacido en el extranjero (PE-libro-tomo). Ej: PE-1234-12345
- Extranjero con cédula (E-libro-tomo). Ej: E-1234-12345
- Naturalizado (N-libro-tomo). Ej: N-1234-12345
- Panameños nacidos antes de la vigencia (provinciaAV-libro-tomo). Ej: 1AV-1234-12345
- Población indigena (provinciaPI-libro-tomo). Ej: 1PI-1234-12345
Identificación de las provincias:
- 1: Bocas del Toro
- 2: Coclé
- 3: Colón
- 4: Chiriquí
- 5: Darién
- 6: Herrera
- 7: Los Santos
- 8: Panamá
- 9: Veraguas
- 10: Guna Yala
- 11: Emberá Wounaan
- 12: Ngäbe-Buglé
- 13: Panamá Oeste
:param id:
:return: object
{
is_valid: True|False, # true if the string could be a valid id, useful while typing.
input: str, # Input value.
is_complete: True|False, # boolean that tells if it is a complete id.
id: ["1","2","3","4"] # Array with the separated components of the id (province, letters, book, volume).
# null - if isValid == false.
# [undefined, undefined, undefined, undefined] - if is_complete == False.
# ["1","2","3","4"] - Values of the id if it is valida and complete (is_valid == is_complete == True).
}
"""
matched = match(FULL_VALID, id)
is_complete = False
groups = None
if matched is not None:
groups = list(matched.groups())
print('groups', groups)
if groups[0] is not None:
is_complete = True
if match(LETTER, groups[0]) is not None:
groups.insert(0, '0')
if match(NUMBER, groups[0]) is not None:
groups.insert(1, '')
if match(MIXED_ID, groups[0]) is not None:
tmp = match('(\d+)(\w+)', groups[0])
groups.insert(0, tmp[1])
groups.insert(1, tmp[2])
return {
'is_valid': True if len(id) == 0 else matched is not None,
'input': id,
'is_complete': is_complete,
'id': groups if is_complete else None,
'unified': '0{}'.format('0'.join(groups)) if is_complete else None
}
Al poner 8- lo valida como cierto.
<input class="input" type="text" placeholder="" pattern="/^P$|^(?:PE|E|N|[23456789]|[23456789](?:A|P)?|1[0123]?|1[0123]?(?:A|P)?)$|^(?:PE|E|N|[23456789]|[23456789](?:AV|PI)?|1[0123]?|1[0123]?(?:AV|PI)?)-?$|^(?:PE|E|N|[23456789](?:AV|PI)?|1[0123]?(?:AV|PI)?)-(?:\d{1,4})-?$|^(PE|E|N|[23456789](?:AV|PI)?|1[0123]?(?:AV|PI)?)-(\d{1,4})-(\d{1,6})$/i" required>
Zubin me facilitó este codigo. Pendiente de agregar al repo:
Función PHP "validate_id":
function validate_id($id){
$a = "";
$b = "";
$c = "";
$group = [];
$isValid = "False";
$isComplete = "False";
$unified = "";
$result = [];
if(preg_match($pattern, $id, $coincidence, PREG_OFFSET_CAPTURE)){
$isValid = "True";
$isComplete = "True";
array_push($group, $coincidence[1][0], $coincidence[2][0], $coincidence[3][0]); //Agregando valores al arreglo $grupos
// PI and AV
if(preg_match('/^(1[0123]?|[23456789])(AV|PI)$/', $group[0], $coincidence, PREG_OFFSET_CAPTURE)){
if($coincidence[2][0] == 'PI' || $coincidence[2][0] == 'AV'){
$a = "0".$group[0];
}
}
// PE
if($group[0] == "PE" ){
$a = str_repeat("\x20", 2).$group[0];
// E
}elseif($group[0] == "E"){
$a = str_repeat("\x20", 2).$group[0].str_repeat("\x20", 1);
// N
}elseif($group[0] == "N"){
$a = str_repeat("\x20", 2).$group[0].str_repeat("\x20", 1);
// 1 - 9
}elseif ($group[0] >= 1 && $group[0] <= 9){
$a = "0".$group[0].str_repeat("\x20", 2);
// two digits
}elseif($group[0] > 9){
$a = $group[0].str_repeat("\x20", 2);
}
// Giving unified format
if(strlen($group[1]) <= "4"){
if(strlen($group[1]) === "1"){
$b = "0"."0"."0".$group[1];
}elseif(strlen($group[1]) == "2"){
$b = "0"."0".$group[1];
}elseif(strlen($group[1]) == "3"){
$b = "0".$group[1];
}elseif(strlen($group[1]) == "4"){
$b = $group[1];
}
}
// Giving unified format
if((strlen($group[2])) <= "5"){
if((strlen($group[2])) == "1"){
$c = "0"."0"."0"."0".$group[2];
}elseif(strlen($group[2]) == "2"){
$c = "0"."0"."0".$group[2];
}elseif(strlen($group[2]) == "3"){
$c = "0"."0".$group[2];
}elseif(strlen($group[2]) == "4"){
$c = "0".$group[2];
}elseif(strlen($group[2]) == "5"){
$c = $group[2];
}
}
//assigning unified value
$unified = $a.$b.$c;
//assigning values to array
$result['isValid'] = $isValid;
$result['isComplete'] = $isComplete;
$result['id'] = $group[0]."-".$group[1]."-".$group[2];
$result['unified'] = $unified;
}else{
//assigning values to the array, only if an "id" is entered with error
$result['isValid'] = $isValid;
$result['isComplete'] = $isComplete;
$result['id'] = "None";
$result['unified'] = "None";
}
//array is returned
return $result;
Hola @merlos y cualquier otro interesado. Voy a estar trabajando en un wrapper para React, por si alguien le interesa. La idea también sería que pudiésemos tener un UMD con ese proyecto, con lo cual sería más fácil trabajar con él.
Por ahora llevo esto (está WIP):
https://github.com/demogar/cedula-panama-field
Quedo atento a cualquier comentario.
Tomando como referencia que la cédula es el identificador único de Personas Naturales, has tomado en consideración, ya sea en este repositorio (o bien en otro), trabajar / incorporar un validador para RUCs como identificador único de Personas Jurídicas?.
Saludos.
Panameño nacido en el extranjero (PE is legacy):
http://www.tribunal-electoral.gob.pa/aclaracion-inscripciones-panamenos-nacidos-exterior/
Extranjero con cédula (E/N-libro-tomo is legacy): E-Provincia-Tomo is the new pattern.
With your actual logic a "E/N" cedula like: E-8-111111 would be invalid. So you'll have to support legacy and new pattern.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.