Differentiate empty input from input with value 0

Asked

Viewed 35 times

-1

Good evening, I am developing a small project that calculates some physics movements, like uniform movement, circular etc.

inserir a descrição da imagem aqui

The intention is to calculate in real time, that is, after the user type in a gap, if possible, the program completes the others automatically, calculating. But when you type something and then delete it, leaving it empty, he understands that the value of that gap is 0, and no longer needs to complete it, because it has already been provided (the program understands that it is empty and needs to be calculated when it is == Undefined).

I could make a code saying that when the input is 0, it gets Undefined, but when the user really wants to provide the value 0, the program will find that it needs to calculate that gap, when in fact it doesn’t need :P

There is a way to differentiate an empty input from an input with 0?

1 answer

0

The type of the value of an input is always a string, so an unfilled input will have '', as an input filled in with 0 will possess '0'.

The fact that you are not able to differentiate an empty string from a string with 0 makes me believe that you are converting this string to a numerical value before you even check if it is empty, and how Number('') results in 0, you won’t be able to differentiate these two values with this approach.

So basically you need to check that the value of your input is not equal to '' before converting it to a numerical value.

  • That’s it! Thank you very much, it worked!

Browser other questions tagged

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