Javascript: The result is displayed even without data entered by the user!

Asked

Viewed 36 times

0

Well, the idea was the following: make a simple function that computes the value inserted in an input and return the result to the user, but if the value entered is zero, the message that returns is: "Blank value!" ; (Because the " when converted to number becomes zero). In short, is there a way that it works there or I’ll have to rewrite in another way.

function inp() {

    var num = Number(input.value);
    var myNum = (num == " ");

    if (Number.isNaN(num) == true) {
        alert("Formato Inválido!");
    } else if (myNum === true) {
        alert("Valor em branco!")
    } else {
        alert((50 + 50 + 50) * num);
    }
}

1 answer

0


You can use a ternary to define num:

function inp() {

    var input = document.querySelector("#ip");
    var num = input.value == '0' ? '0' : Number(input.value);
    var myNum = (num == " ");

    if (Number.isNaN(num) == true) {
        alert("Formato Inválido!");
    } else if (myNum === true) {
        alert("Valor em branco!")
    } else {
        alert((50 + 50 + 50) * num);
    }
}
<input type="text" id="ip" />
<input type="button" value="ok" onclick="inp()" />

  • Gee, thanks anyway! I’m new on the platform so I can’t vote as useful, but you’re f*.

  • You can mark it right

  • Below the arrows there is a symbol " "... but you can only mark this in 1 answer, the one you prefer (the one that helped you most, thought better, love correct for you etc...)

Browser other questions tagged

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