Knowing the input data

Asked

Viewed 54 times

-2

Hello, I need to know what kind of data is being inserted into the HTML input, I have to know if what is there is text(string) or if it is a number. And then I create a vector using the slipt. It’s always returning Number

let valores = $('#valores').val();
if(isNaN(valores) === true){      
    console.log('Número');
} else {
    console.log('Letra');
}
let dados = valores.split(';');
  • Hello from a read on this link may be a light for your question, try to edit your post and add more details. already good luck! https://forum.imasters.com.br/topic/537487-como-verificar-tipo-de-valor-inserido-no-input/

  • What values did you use to test? If you are going to split later, then you will not only have numbers, please edit the question and put the strings you are using and what should be the result

  • 1

    Matheus, do you realize that NaN means "Not a Number"? That is, your if makes "If valores is not a number, log 'Number'"... It makes no sense

1 answer

1

Try the code below:

var myVar = prompt("Digite um texto ou número.");

if(!myVar){
alert("Operação cancelada pelo usuário");
}
else{

while(myVar.trim().length == 0){
myVar = prompt("Digite um texto ou um número.");
}

if(!isNaN(myVar)){
alert(myVar + " é um número");
}
else{
alert(myVar + " é uma String");
}
}

Browser other questions tagged

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