Shadcn <Input> with type="number" returns string
Answered
West African Lion posted this in #help-forum
West African LionOP
Did anyone have the same issue? Why does the input always return a string, even when I set the type to number?
Answered by Black Turnstone
It is the normal behaviour, type="number" is for devices with virtual keyboard to show numpad instead of whole keyboard.
6 Replies
@West African Lion Did anyone have the same issue? Why does the input always return a string, even when I set the type to number?
Black Turnstone
It is the normal behaviour, type="number" is for devices with virtual keyboard to show numpad instead of whole keyboard.
Answer
@Black Turnstone It is the normal behaviour, type="number" is for devices with virtual keyboard to show numpad instead of whole keyboard.
West African LionOP
So, I have to manually parse to number/int?
Black Turnstone
Yes
If you are using zod and input in form you can do something like this in your zod schema.
const input = z.coerce.number()
@Black Turnstone If you are using zod and input in form you can do something like this in your zod schema.
ts
const input = z.coerce.number()
West African LionOP
this solves the issue, thank you
Rose-breasted Grosbeak
value
attribute is a string value.https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/valueAsNumber