Add national ID document validation

This commit is contained in:
coolneng 2020-06-18 07:35:57 +02:00
parent 68b528b960
commit 322a464eb7
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
2 changed files with 20 additions and 9 deletions

View File

@ -7,6 +7,7 @@
<link rel="stylesheet" href="../../static/style.css" type="text/css" media="screen" /> <link rel="stylesheet" href="../../static/style.css" type="text/css" media="screen" />
<script src="../../static/jquery-3.5.1.min.js"></script> <script src="../../static/jquery-3.5.1.min.js"></script>
<script src="../ajax.js"></script> <script src="../ajax.js"></script>
<script src="../validate_id.js"></script>
</head> </head>
<body> <body>
<?php <?php
@ -15,7 +16,7 @@
$pdo = connectDatabase(); $pdo = connectDatabase();
$regions = fetchRegions($pdo); $regions = fetchRegions($pdo);
?> ?>
<form method="post" action="../patient_management.php"> <form name="create_form" method="post" action="../patient_management.php" onsubmit="return validateDNI();">
<div class="input-group"> <div class="input-group">
<label>Nombre</label> <label>Nombre</label>
<input type="text" name="nombre" value=""> <input type="text" name="nombre" value="">

View File

@ -1,19 +1,19 @@
function validateDNI(dni) { function checkDNI(dni) {
var number, let, letra; var number, let, letter;
var expresion_regular_dni = /^[XYZ]?\d{5,8}[A-Z]$/; var regex = /^[XYZ]?\d{5,8}[A-Z]$/;
dni = dni.toUpperCase(); dni = dni.toUpperCase();
if(expresion_regular_dni.test(dni) === true){ if(regex.test(dni) === true){
number = dni.substr(0,dni.length-1); number = dni.substr(0,dni.length-1);
number = number.replace('X', 0); number = number.replace('X', 0);
number = number.replace('Y', 1); number = number.replace('Y', 1);
number = number.replace('Z', 2); number = number.replace('Z', 2);
let = dni.substr(dni.length-1, 1); let = dni.substr(dni.length-1, 1);
number = number % 23; number = number % 23;
letra = 'TRWAGMYFPDXBNJZSQVHLCKET'; letter = 'TRWAGMYFPDXBNJZSQVHLCKET';
letra = letra.substring(number, number+1); letter = letter.substring(number, number+1);
if (letra != let) { if (letter != let) {
return false; return false;
}else{ }else{
return true; return true;
@ -21,3 +21,13 @@ function validateDNI(dni) {
return false; 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;
}