0

I have a Form.Input which has type="number". When I try to verify that the Input is empty, it gives me true when the value is 0 (number, if it is "0" as a String it does give false).

if (!this.state.invoice.reimbursed) {
  errors.push({
    key: "reimbursed",
    text: "Reimbursed is required"
  });
}

This does work correctly with other fields, I just have a problem with the one that has type="number".

Am I doing the verification wrong?

Valenti
  • 3
  • 2
  • 2
    Number 0 is falsy. – pavi2410 Nov 02 '18 at 10:59
  • 1
    In JavaScript `0`, `null`, `false` and `undefined` are all evaluated to false when in a condition. You can check this out in the browser console. Check out the answers here for more details https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons – Blagoj Atanasovski Nov 02 '18 at 11:01
  • `0` (the number) is falsy, but the string `"0"` is truthy. By default the input value will be a string, are you converting it to a number somewhere? – Robin Zigmond Nov 02 '18 at 11:02
  • @Pavitra I see, but then how would I verify that this field is not empty? I tried lodash without success... – Valenti Nov 02 '18 at 11:04
  • @RobinZigmond when I input a 0 it is a String and all is good. The problem is when I load the page, the input gets filled with what is in the DB, which is a number, if I then don't change the input it will stay a number, and thus this problem. – Valenti Nov 02 '18 at 11:05
  • 2
    You can do something like this `new String(number).length !== 0` – pavi2410 Nov 02 '18 at 11:13
  • @Pavitra that works just fine, thanks for the speedy solution! – Valenti Nov 02 '18 at 11:18
  • Pavitra is right. You have to treat the number like a string in order to see if it is empty. But here's what I don't understand, this.state.invoice.reimbursed doesn't look like an input[type='number'] element. It looks like a raw value of a plain old JavaScript object. Is that the case? – Aron Boyette Nov 02 '18 at 11:23
  • Ah! Well, never mind! Good show, Pavitra. – Aron Boyette Nov 02 '18 at 11:24
  • @AronBoyette it's a value in the state that is handled by an Input. – Valenti Nov 02 '18 at 11:28

1 Answers1

0

As mentioned by multiple comments, the number 0 is falsy in JavaScript - thus a number field holding the value 0 is interpreted as if it was holding null / false / undefined - as if it was empty (which it technically is; if you ask the user for a number and they enter 0, it's likely to be useless information)

d0n.key
  • 1,318
  • 2
  • 18
  • 39
  • 1
    `if you ask the user for a number and they enter 0, it's likely to be useless information` no, it's not going to be. Maybe in some circumstances but it's havily context dependent. You can also ask a lot of questions where zero is a perfectly normal and acceptable answer. Like "How many people live with you?" or "How many years have you done X?" and so on. – VLAZ Nov 02 '18 at 11:46
  • @vlaz You're correct, but if you wouldn't fill in the two provided example inputs, it would result in the same answer: "None", "Non-existant" etc. I mean, that is pretty much what a 0 stands for, in most cases you could just leave the field blank instead of typing a 0. (from an ethical point of view) – d0n.key Nov 02 '18 at 12:40