How to check if one variable is INT and the other is a String?

Asked

Viewed 69 times

-1

I’m new to Java and I’m trying to make a game... I need to verify that the two data inputs are int and string. "value1" has to be integer and "value2" has to be a string. Can you tell me if the logic is right?

if((valor1== "") || (valor2== "")){
    alert("Prencha os campos corretamente...");
}else{
    if(((valor1%2) != NULL) && ((valor2%2) == NULL)){
        // chama outra função....
    }
}

1 answer

1


Check the variable type using the command typeof, store the value in a variable and check if the value is what you want. Follow a short example:

valor1 = 1;
valor2 = '1';

tipoValor1 = typeof valor1;
tipoValor2 = typeof valor2;

console.log('tipo valor1: '+ tipoValor1);
console.log('tipo valor2: '+ tipoValor2);

if((tipoValor1 != 'number') || (tipoValor2 != 'string')){
    alert("Prencha os campos corretamente...");
}else{
    console.log('pode executar');
}

If you are capturing form with pure javascript, it is likely that you will need to give a parseInt in its variable which must be number. Example: valor1 = parseInt('1');.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.