Problem validating whether or not it is a number

Asked

Viewed 50 times

1

function isNumber(n){
	if(typeof n == "number"){
		return true
	} else{
		return false
	}
}









function contador(){
	let numero = document.getElementById('num')
	let num = Number(numero.value)
	let vet = []

	if(isNumber(num)){
		alert('É número!!!')
	} else{
		alert('Não é numero ou não foi digitado um numero')
	}
}
body{
    background: #D96E48;
}
header{
    color: white;
    text-align: center;
}
section{
    background: white;
    border-radius: 10px;
    padding: 15px;
    margin: auto;
    width: 500px;
    box-shadow: 3px 3px 15px rgba(0,0,0.363);
}
footer{
    color: white;
    text-align: center;
    font-style: italic;
}

#box{
    margin: 0 0 0 125px;
    width: 250px;
}
<!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Contador</title>
        <link rel="stylesheet" href="estilo.css">
    </head>
    <body>
        <!-- Cabeçalho -->
        <header>
            <h1>Contador</h1>
        </header>

        <!-- Conteúdo Principal -->
        <section>
            <div>
                <p>Digite um numero (1 a 100):
                    <input type="number" name="num" id="num">
                    <input type="button" value="resultado" onclick="contador()">
                </p>
                <p>
                    <select name="box" id="box" size="10"></select>
                </p>
            </div>
        </section>

        <!-- Rodapé -->
        <footer>
            <p>&copy; CursoemVideo</p>
        </footer>

        <script src="script.js"></script>

    </body>
</html>

  • Welcome. What exactly is the problem? What error does it make? Have a look at tour for some very useful tips on question formulation. : D

  • 1

    if(isNumber(n)) - in this passage, there is no variable n defined, try to exchange for if(isNumber(num))

  • Hello, please take a look at a previous post https://answall.com/questions/11275/verifi-se-string-posses-apenas-n%C3%Bameros, this link explains how to use isNaN to know if the string contains only numbers.

1 answer

4

TL;DR: Use the method Number.isNaN.


You’re trying to use the builder Number together with typeof to validate a number. However, this approach does not work, since:

  • The builder Number returns NaN when the value passed is not numerical.
  • typeof NaN is number.

Therefore, passing a number or something other than a number, the validation will not work due to the operator’s misuse typeof.

Behold:

console.log(typeof NaN);
console.log(typeof Number(5));
console.log(typeof Number('Luiz'));

Having this in mind, instead of using the operator typeof, prefer to use the method Number.isNaN to perform the verification:

function isNumber(val) {
  return !Number.isNaN(Number(val));
}

console.log(5, isNumber(5));
console.log(NaN, isNumber(NaN));
console.log('4', isNumber('4'));
console.log('Luiz', isNumber('Luiz'));

Browser other questions tagged

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