我一直在寻找针对西类牙ID(例如NIE,NIF或CIF)的验证器,但找不到用Groovy
编写的验证器。这就是为什么我在这里。我在javascript(http://www.yporqueno.es/blog/as3-validar-nif-cif-y-nie)中有此代码,以说明如何执行此操作。有人已经将其转录为Groovy
吗?
最佳答案
这是我的课:
class TestDniService {
//--------------------------------------------------------------------------
public Boolean isNif(String a) {
int numero
String dni=a.toUpperCase();
// def letra = dni.substring(dni.length())
try{
println"try "+dni.substring(0,dni.length()-1)
numero =dni.substring(0,dni.length()-1).toInteger()
}catch (NumberFormatException num){
return false
}
String let = dni.substring(dni.length()-1)
let=let.toUpperCase();
numero=numero%23;
String letra='TRWAGMYFPDXBNJZSQVHLCKET';
String lletra =letra.charAt(numero);
return (lletra == let);//devuelve true o false
}//End isNif
//--------------------------------------------------------------------------
public Boolean isNie(String a) {
String dni=a.toUpperCase();
String pre=dni.substring(0,1);
String prev="0";
if (pre=="X") {
prev="0";
} else if (pre == "Y") {
prev="1";
} else if (pre == "Z") {
prev="2";
}
String numero = prev.toString()+dni.substring(1,dni.length()).toString()
return isNif(numero);//una vez tratamos el NIE lo comprovamos como un NIF
}//End isNie
//--------------------------------------------------------------------------
public Boolean isCif(String cif) {
println"CIF inicial "+cif
def pares = 0;
def impares = 0;
def suma;
def ultima;
def unumero;
def uletra = ["J", "A", "B", "C", "D", "E", "F", "G", "H", "I"]
def xxx;
String texto=cif.toUpperCase()
if (!texto ==~ /^[ABCDEFGHJKLMNPQS]\d{7}[0-9,A-J]$/) {
return false;
}
ultima = texto.substring(8,9)
pares = texto.substring(2,3).toInteger()+texto.substring(4,5).toInteger()+texto.substring(6,7).toInteger()
for (int cont = 1 ; cont <= 7 ; cont++){
xxx = 2 * texto.substring(cont,cont+1).toInteger()
if (xxx > 9) while (xxx > 9){
xxx = xxx.toString().substring(0,1).toInteger()+ xxx.toString().substring(1,2).toInteger()
}
impares += xxx
cont++
}
suma = (pares + impares).toString();
unumero = suma.substring(suma.length() - 1,suma.length()).toInteger()
unumero = (10 - unumero).toString();
if (unumero.toInteger() == 10) unumero = 0;
if ((ultima.toString() == unumero.toString()) || (ultima == uletra[unumero.toInteger()]))
return true;
else
return false;
}//End isCif
//--------------------------------------------------------------------------
public int testAll(String dni) {
String cadena = dni.toUpperCase();//convertimos a mayusculas
String pre= cadena.substring(0,1);//extraemos el primer digito o letra
if (pre=="X"||pre=="Y"||pre=="Z") {//Si el primer digito es igual a X,Y o Z entonces es un NIE
if (isNie(dni)) {//llamamos a la funcion testNIE(); pasandole por parametro el dni. Devolvera true o false
return 1;//Si es true devolvemos 1, 1 = NIE correcto.
} else {
return -1;//Si es false devolvemos -1, -1 = NIE incorrecto.
}
} else {//Si no es un NIE comprovamos si es un CIF
if (pre ==~ (/[ABCDEFGHJKLMNPQRSUVW]/)) {//Si la primera letra de la cadena coincide con alguna del patron letrasCIF entonces es un CIF
if (isCif(dni)) {//llamamos a la funcion testCIF(); pasandole por parametro el dni. Devolvera true o false
return 2;//Si es true devolvemos 2, 2 = CIF correcto.
} else {
return -2;//Si es false devolvemos -2, -2 = CIF incorrecto.
}
} else {//Si no es un CIF comprovamos si es un NIF
if (pre ==~ (/[1234567890]/)) {//Si el primer digito de la cadena coincide con el patron numerosNIF entonces es un NIF
if (isNif(dni)) {//llamamos a la funcion testNIF(); pasandole por parametro el dni. Devolvera true o false
return 3;//Si es true devolvemos 3, 3 = NIF correcto.
} else {
return -3;//Si es false devolvemos -3, -3 = NIF incorrecto.
}
} else {//Si tampoco es un NIF entonces no es un dni valido de ningun tipo
//si no es ninguno devolvemos 0
return 0;
}
}
}
}//End function test
}
我希望它会帮助任何人。@gsan提供的答案
关于validation - 西类牙语ID:grails中的NIF,NIE,CIF验证程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34438372/