Created
February 22, 2017 13:20
-
-
Save osvik/a2c090f4a64f75318b3af7ddf854f1b7 to your computer and use it in GitHub Desktop.
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
/** | |
* @namespace Custom validation related | |
*/ | |
var customValidation = { | |
/** | |
* Known str_replace funcion. Required in valida_nif_cif_nie | |
*/ | |
str_replace: function(e, t, n) { | |
var j; | |
var r = e, | |
i = t, | |
s = n; | |
var o = i instanceof Array, | |
u = s instanceof Array, | |
r = [].concat(r), | |
i = [].concat(i), | |
a = (s = [].concat(s)).length; | |
while (j = 0, a--) { | |
if (s[a]) { | |
while (s[a] = s[a].split(r[j]).join(o ? i[j] || "" : i[0]), ++j in r) {} | |
} | |
} | |
return u ? s : s[0]; | |
}, | |
/** | |
* Validates Spanish ID number | |
* @param {string} value DNI, NIE or CIF | |
* @returns {number} 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, -1 = NIF error, -2 = CIF error, -3 = NIE error, 0 = ??? error | |
*/ | |
validatesDniNifCif: function(value) { | |
var j, posicion, letra, suma, i, temp1, temp2, n, pos; | |
var a = this.cleanUpString(value); | |
var temp = this.cleanUpString(value); | |
var cadenadni = "TRWAGMYFPDXBNJZSQVHLCKE"; | |
if (temp !== '') { | |
// If it doesn't has a valid format, returns error | |
if ((!/^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$/.test(temp) && !/^[T]{1}[A-Z0-9]{8}$/.test(temp)) && !/^[0-9]{8}[A-Z]{1}$/.test(temp)) { | |
return 0; | |
} | |
// Standard NIFs | |
if (/^[0-9]{8}[A-Z]{1}$/.test(temp)) { | |
posicion = a.substring(8, 0) % 23; | |
letra = cadenadni.charAt(posicion); | |
var letradni = temp.charAt(8); | |
if (letra == letradni) { | |
return 1; | |
} else { | |
return -1; | |
} | |
} | |
// Type CIF codes | |
suma = parseInt(a[2]) + parseInt(a[4]) + parseInt(a[6]); | |
for (i = 1; i < 8; i += 2) { | |
temp1 = 2 * parseInt(a[i]); | |
temp1 += ''; | |
temp1 = temp1.substring(0, 1); | |
temp2 = 2 * parseInt(a[i]); | |
temp2 += ''; | |
temp2 = temp2.substring(1, 2); | |
if (temp2 === '') { | |
temp2 = '0'; | |
} | |
suma += (parseInt(temp1) + parseInt(temp2)); | |
} | |
suma += ''; | |
n = 10 - parseInt(suma.substring(suma.length - 1, suma.length)); | |
// Special NIFs (Calculated as CIFs) | |
if (/^[KLM]{1}/.test(temp)) { | |
if (a[8] == String.fromCharCode(64 + n)) { | |
return 1; | |
} else { | |
return -1; | |
} | |
} | |
// CIF | |
if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(temp)) { | |
temp = n + ''; | |
if (a[8] == String.fromCharCode(64 + n) || a[8] == parseInt(temp.substring(temp.length - 1, temp.length))) { | |
return 2; | |
} else { | |
return -2; | |
} | |
} | |
// NIE | |
// T | |
if (/^[T]{1}/.test(temp)) { | |
if (a[8] == /^[T]{1}[A-Z0-9]{8}$/.test(temp)) { | |
return 3; | |
} else { | |
return -3; | |
} | |
} | |
// XYZ | |
if (/^[XYZ]{1}/.test(temp)) { | |
pos = this.str_replace(['X', 'Y', 'Z'], ['0', '1', '2'], temp).substring(0, 8) % 23; | |
if (a[8] == cadenadni.substring(pos, pos + 1)) { | |
return 3; | |
} else { | |
return -3; | |
} | |
} | |
} | |
return 0; | |
} | |
} | |
jQuery.validator.addMethod("es_nifcifnie", function(value, element) { | |
return this.optional(element) || customValidation.validatesDniNifCif(value) >= 1; | |
}, "Valid Spanish NIF, NIE or CIF for example 82451384H or X6909535J"); | |
jQuery.validator.addMethod("nocif", function(value, element) { | |
return this.optional(element) || customValidation.validatesDniNifCif(value) != 2; | |
}, "Just Spanish DNI or NIE please, no CIF"); |
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
<?php | |
class Validator { | |
/** | |
* DNI/NIE validator | |
* | |
* @param string $dni Unfiltered DNI | |
* @return bolean If it's a DNI/NIE or not | |
*/ | |
public static function dni($dni) { | |
if(strlen($dni)<9) { | |
return false; | |
} | |
$dni = strtoupper($dni); | |
$letra = substr($dni, -1, 1); | |
$numero = substr($dni, 0, 8); | |
// Si es un NIE hay que cambiar la primera letra por 0, 1 ó 2 dependiendo de si es X, Y o Z. | |
$numero = str_replace(array('X', 'Y', 'Z'), array(0, 1, 2), $numero); | |
$modulo = $numero % 23; | |
$letras_validas = "TRWAGMYFPDXBNJZSQVHLCKE"; | |
$letra_correcta = substr($letras_validas, $modulo, 1); | |
if($letra_correcta!=$letra) { | |
return false; | |
} | |
else { | |
return true; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment