From 322a464eb7508dc99e86fba57b1bc219eb54cc1e Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 18 Jun 2020 07:35:57 +0200 Subject: [PATCH] Add national ID document validation --- src/forms/patient_create_form.php | 5 +++-- src/validate_id.js | 24 +++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/forms/patient_create_form.php b/src/forms/patient_create_form.php index ec6518e..a3b6da1 100644 --- a/src/forms/patient_create_form.php +++ b/src/forms/patient_create_form.php @@ -7,6 +7,7 @@ + -
+
@@ -62,7 +63,7 @@
- +
diff --git a/src/validate_id.js b/src/validate_id.js index b83ca9a..17c4e39 100644 --- a/src/validate_id.js +++ b/src/validate_id.js @@ -1,19 +1,19 @@ -function validateDNI(dni) { - var number, let, letra; - var expresion_regular_dni = /^[XYZ]?\d{5,8}[A-Z]$/; +function checkDNI(dni) { + var number, let, letter; + var regex = /^[XYZ]?\d{5,8}[A-Z]$/; dni = dni.toUpperCase(); - if(expresion_regular_dni.test(dni) === true){ + if(regex.test(dni) === true){ number = dni.substr(0,dni.length-1); number = number.replace('X', 0); number = number.replace('Y', 1); number = number.replace('Z', 2); let = dni.substr(dni.length-1, 1); number = number % 23; - letra = 'TRWAGMYFPDXBNJZSQVHLCKET'; - letra = letra.substring(number, number+1); - if (letra != let) { + letter = 'TRWAGMYFPDXBNJZSQVHLCKET'; + letter = letter.substring(number, number+1); + if (letter != let) { return false; }else{ return true; @@ -21,3 +21,13 @@ function validateDNI(dni) { return false; } } + +function validateDNI(){ + var dni = document.forms["create_form"]["documento_identificativo"]; + if(!checkDNI(dni.value)){ + alert("El DNI/NIE no es correcto"); + return false; + } + alert("El DNI/NIE es correcto"); + return true; +}