question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty input type=number results in NaN

See original GitHub issue

I’m using Formik with Yup Schema validation.

I have an input field of type “number”. If I enter a value and empty the field this results in a NaN. This behavior might be as expected. For that I added a transform function to my schema like that:

yup
.number()
.transform(value => {
  console.log(value, Number.isNaN(value) ? 0 : value 
  return Number.isNaN(value) ? 0 : value
})

Should it respect the transform function? Or is the schema actually only be used for validation, not for “cleaning” the data? If so, is there a way how to solve that? Maybe I’m missing something. Of course, I can check the value before attaching it to the input field. But the value in the global values object stays the same.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
nhjhncbcommented, Aug 3, 2018

try to use setFieldValue

onChange={e => setFieldValue('condition.operand', +e.target.value)}
0reactions
EmirGluhbegoviccommented, Jul 25, 2018

I am having the same issue, has this been fixed or is there some recommended way of dealing with this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Differentiate between NaN input and empty input with an Input ...
The problem with this is that I want to differentiate between the user entering an empty string (""), and the user entering a...
Read more >
NaN - JavaScript - MDN Web Docs
The global NaN property is a value representing Not-A-Number. ... Math operation where the result is not a real number (e.g. Math.sqrt(-1) ) ......
Read more >
Can I prevent "NaN" from appearing in numerical entry?
The logic is based on a boolean "isNaN" flag variable that gets initialized to true, and is then reset to false if the...
Read more >
Create array of all NaN values - MATLAB NaN - MathWorks
X = NaN returns the scalar representation of "not a number". Operations return NaN when they have undefined numeric results, such as 0/0...
Read more >
Convert NaN to 0 using JavaScript | bobbyhadz
Use the logical or operator to convert `NaN` to `0`, e.g. `const result = val || 0;`. ... undefined , false , 0...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found