Check that the value of a field complies with at least one of 3 conditions

5

I am doing a program which you need to put the Spanish document (NIF, NIE, CIF) and I need to check that the data entered are correct.

The user enters the value in a single input and must match the NIF, or NIE, or CIF format.

This is the code I have so far:

if (document.formProveedor.NIF.value!="") {
    var regNIF = /^([0-9]){8}([A-Z]){1}$/;
    var regNIE = /^([A-Z]){1}([0-9]){7}([A-Z]){1}$/;
    var regCIF = /^([A-Z]){1}([0-9]){8}}$/;

    if(regNIF.test(document.formProveedor.NIF.value) == false) {        
        alert("El NIF introducido no es correcto.");
        document.formProveedor.NIF.focus();
        return false;   
    }else if(regNIE.test(document.formProveedor.NIF.value) == false){
        alert("El NIE introducido no es correcto.");
        document.formProveedor.NIF.focus();
        return false;
    }else if(regCIF.test(document.formProveedor.NIF.value) == false){
        alert("El CIF introducido no es correcto.");
        document.formProveedor.NIF.focus();
        return false;
    }
}

EXAMPLES:

NIF: 74063793K
NIE: Y9945517D
CIF: G82868043
  • When I fill in the field with a NIF and the function is executed, I skip the alert

      

    "The NIE entered is not correct."

  • When I fill the field with a NIE and the function is executed, I skip the alert

      

    "The NIF entered is not correct."

  • When I fill the field with a CIF and the function is executed, I skip the alert

      

    "The NIF entered is not correct."

asked by hayber 12.04.2018 в 16:37
source

2 answers

0

I have reformulated the way you do it, and in my opinion you should use some type of dropdown to choose what type of validation you want, so you do not have to write many lines comparing only NIF, CIF, NIE, etc.

First of all I have made an array of objects with their type and validation.

var validacion = [
    {
        tipo: 'NIF',
        regex: /^([0-9]){8}([A-Z]){1}$/,
    },
    {
        tipo: 'NIE',
        regex: /^([A-Z]){1}([0-9]){7}([A-Z]){1}$/,
    },
    {
        tipo: 'CIF',
        regex: /^([A-Z]){1}([0-9]){8}}$/,
    },
];

With that I have made a function validar that takes two parameters: tipo and numero , where the type is NIE, NIF, ID, CIF ... and the number the value you want to check.

function validar(tipo, numero) {
  if(tipo == null || numero == null){
    alert('Error en los datos');
    return false;
  }

    var val = validacion.find(x => x.tipo == tipo);

    if (!val.regex.test(numero)) {
        alert('El ' + tipo + ' introducido no es correcto.');
        return false;
    }
}

To call it would be like this:

validar('NIF', '74063693K'); // true
validar('NIF', '740dd693K'); // false
validar('NIE', 'Y9945517D'); // true
validar('NIE', '74063793K'); // false

Remember that to fill in the tipo parameter, it would be nice if you used a dropdown .

    
answered by 12.04.2018 / 18:55
source
4

Your problem is that you look one by one: if you put a NIF, obviously it is not a CIF and it gives you the error, similarly happens with a CIF or a NIE.

The simplest solution would be something like (in pseudo-code):

function validarID(...)
    if (<es un NIF correcto>)
       return OK
    }
    if (<es un CIF correcto>)
       return OK
    }
    if (<es un NIE correcto>)
       return OK
    }

    return NO_OK
}

But a better solution would be to select the validator first, in a select, and apply it to the data entered, because a user could mistakenly enter a valid value for an NIE when he really wants to enter a CIF.

    
answered by 12.04.2018 в 18:12