react-hook-form: an optional number input gives NaN instead of a number or null
The field value is NaN instead of a numberShort 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().
Fix it fast
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));2Match what you see, then jump to its fix
- 1
The logged value is NaN, and the schema or your own check rejects it.valueAsNumber returns NaN for an unconvertible valueFix - 2
Your setValueAs function never runs and the value keeps coming out as before.setValueAs is set together with valueAsNumberFix - 3
The 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
- 1
What does the error look like?
The field value is NaN instead of a numberValidation 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" src2Fix
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
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" src2Fix
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
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" src2Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
The validation message never appears under an MUI X Date Picker
The field never turns red after a failed validation
A DatePicker does not take helperText or error as direct props.
3 causes, each with a fixWeb apps
React Hook Form: the submit button does nothing and onSubmit never runs
The page reloads when the form is submitted
handleSubmit runs your onSubmit only when validation passes.
4 causes, each with a fixWeb apps
React Hook Form isDirty does not go back to false
formState.isDirty is false after setValue
React Hook Form sets isDirty by comparing the current values with defaultValues.
4 causes, each with a fixWeb apps
A Zod refinement error does not appear or does not clear in React Hook Form
The refinement error never shows until every other field is valid
Three Zod and React Hook Form rules explain it.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fix