Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

react-hook-form: an optional number input gives NaN instead of a number or null

ErrorThe field value is NaN instead of a number

Short answer

valueAsNumber returns NaN when a value cannot be converted, and setValueAs is ignored while valueAsNumber is on. For a field that may stay empty, drop valueAsNumber, convert in setValueAs (empty text becomes null, anything else Number), and let the schema accept null with z.number().nullable().

  • Applies to react-hook-form 7, Zod 4, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log what the form really holds for the field before blaming the schema.

    // inside your component
    const values = watch();
    console.log(JSON.stringify(values), Object.is(values.age, NaN));
  2. 2Match what you see, then jump to its fix

    1. 1The logged value is NaN, and the schema or your own check rejects it.valueAsNumber returns NaN for an unconvertible valueFix
    2. 2Your setValueAs function never runs and the value keeps coming out as before.setValueAs is set together with valueAsNumberFix
    3. 3The field converts correctly but validation still fails on empty, or the first render shows the wrong value.The schema does not accept null, or the default value is a stringFix

What does the error look like?

  • The field value is NaN instead of a number
  • Validation fails on an optional numeric field

Cause 1 of 3

valueAsNumber returns NaN for an unconvertible value

1Check

The register docs say valueAsNumber returns a Number normally and NaN if something goes wrong. Look for valueAsNumber on the optional field.

grep -rn "valueAsNumber" src

2Fix

Remove valueAsNumber from the optional field and convert yourself, so an empty box becomes null.

<input
  type="text"
  inputMode="numeric"
  {...register('age', {
    setValueAs: (v) => (v === '' || v == null ? null : Number(v)),
  })}
/>

3Verify

Submit once with the field empty and once with a number. The logged value should be null, then a number.

console.log(getValues('age'));

Confirmed by react-hook-form.com

Did this fix it?

Cause 2 of 3

setValueAs is set together with valueAsNumber

1Check

The register docs state that setValueAs is ignored when valueAsNumber or valueAsDate is true. It is also documented for text inputs.

grep -rn -B2 -A2 "setValueAs" src

2Fix

Use only one of them on a field. For number or null, keep setValueAs and delete valueAsNumber.

register('age', {
  setValueAs: (v) => (v === '' ? null : Number(v)),
})

3Verify

Add a console.log inside setValueAs and type in the field; it should print.

setValueAs: (v) => { console.log('raw', v); return v === '' ? null : Number(v); }

Confirmed by react-hook-form.com

Did this fix it?

Cause 3 of 3

The schema does not accept null, or the default value is a string

1Check

Neither valueAsNumber nor setValueAs transforms defaultValues, so an empty default stays as you wrote it. Zod's z.number() also rejects null unless it is wrapped.

grep -rn "defaultValues" src

2Fix

Declare the field as nullable in the schema and give it a null default.

const schema = z.object({
  age: z.number().nullable(),
});

useForm({
  resolver: zodResolver(schema),
  defaultValues: { age: null },
});

3Verify

Submit with the field empty and confirm the form passes validation.

console.log(formState.errors.age);

Confirmed by zod.dev

Did this fix it?

How do I stop it coming back?

  • Pick one conversion path per field: valueAsNumber for required numbers, setValueAs for optional ones.
  • Write the empty case into the schema and the default value at the same time.
  • Keep the form type and the API type separate when the server wants numbers and the form works with text.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.