0

I this is really simple but I'm stuck.

I have a form in a React app that creates errors, the error codes could be

'incomplete_number' || 'incomplete_expiry' || 'incomplete_cvc' || 'incomplete_zip'

There are other errors but I don't want to show the error above.

I have a useState to capture the errors so I'm trying to populate it if the error is NOT one of the above

if (error) {
    // eslint-disable-next-line no-restricted-syntax
    if (error.code !== 'incomplete_number' || 'incomplete_expiry' || 'incomplete_cvc' || 'incomplete_zip') {
        setErrorMessage(error.message)
    }
}

This code doesn't work, but for just one it does

if (error) {
    // eslint-disable-next-line no-restricted-syntax
    if (error.code !== 'incomplete_number') {
        setErrorMessage(error.message)
    }
}

How can I test for all the errors

lomine
  • 873
  • 5
  • 20
  • 36
  • That's not how to do multiple comparisons in JavaScript. You have to compare `error.code` to each string individually. – Pointy Aug 15 '23 at 14:10
  • 1
    Computer languages are not human languages. There is no room for intuition and context. What you have are four boolean conditions, three of which are nothing more than a literal string value which is always "truthy" so the overall condition will always be `true`. – David Aug 15 '23 at 14:12

0 Answers0