Don't want decimal numbers in form input to get accepted
Answered
QBit posted this in #help-forum
![Avatar](https://cdn.discordapp.com/avatars/858742600550580234/0ca04d9e816fd1fbf45a9716c47e607a.webp?size=256)
QBitOP
Form should not accept decimal nos.
For <=0 numbers code is written to not accept them. Now it accepting positive decimal also but I don't want. What to do?
For <=0 numbers code is written to not accept them. Now it accepting positive decimal also but I don't want. What to do?
"use client"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { useRouter } from "next/navigation"
import { Button } from "@/components/ui/button"
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
const FormSchema = z.object({
ID: z
.string()
.min(1, { message: "Problem ID must be at least 1 character" })
.refine((value) => parseInt(value) > 0, {
message: "ID must be greater than zero",
}),
})
export function ProblemIDForm() {
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
ID: undefined,
},
})
const router = useRouter()
function onSubmit(values: z.infer<typeof FormSchema>) {
// console.log(values)
router.push(`/problems/${values.ID}`)
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="w-2/3 space-y-6">
<FormField
control={form.control}
name="ID"
render={({ field }) => (
<FormItem>
<FormLabel className="flex justify-center">Problem ID</FormLabel>
<FormControl>
<Input type="number" placeholder="Enter ID here" {...field} />
</FormControl>
<FormDescription>LeetCode problem ID</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<div className="flex justify-center">
<Button type="submit">Submit</Button>
</div>
</form>
</Form>
)
}
14 Replies
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
what do you want the value to be?
positive nonnegative integers?
![Avatar](https://cdn.discordapp.com/avatars/484037068239142956/f386ac21405ce3442e0f3598223e1cb3.webp?size=256)
joulev
Number.isInteger() to check if a number is an integer
![Avatar](https://cdn.discordapp.com/avatars/484037068239142956/f386ac21405ce3442e0f3598223e1cb3.webp?size=256)
joulev
Ackshually, zod has native methods for this
z.number().int().positive().finite() is probably what you need
![Avatar](https://cdn.discordapp.com/avatars/858742600550580234/0ca04d9e816fd1fbf45a9716c47e607a.webp?size=256)
QBitOP
when I changed the zod
Now the integer is not getting accepted
I think my
FormSchema
to this @joulev const FormSchema = z.object({
// ID: z
// .string()
// .min(1, { message: "Problem ID must be at least 1 character" })
// .refine((value) => parseInt(value) > 0, {
// message: "ID must be greater than zero",
// }),
ID: z.number().int().positive().finite(),
})
Now the integer is not getting accepted
I think my
z.number()
is not working. Something maybe mismatched![Image](https://cdn.discordapp.com/attachments/1240312416336613467/1240340399516815391/Screenshot_from_2024-05-15_21-58-42.png?ex=66463483&is=6644e303&hm=4a7f1ab67fc40b5e2e441507a663d20152cda8efd89eea5e0d62cdbd29497e0b&)
I want to accept only natural numbers
I don't want
1, 2, 3,....
I don't want
23.45, 38.6, ....
such decimal numberscurrently form is accepting pos decimal also
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
you need to transform the output into number first
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
try
z.coerce.number().int().positive().finite()
Answer
![Avatar](https://cdn.discordapp.com/avatars/858742600550580234/0ca04d9e816fd1fbf45a9716c47e607a.webp?size=256)
QBitOP
working!!
![Image](https://cdn.discordapp.com/attachments/1240312416336613467/1240343432103329843/image.png?ex=66463756&is=6644e5d6&hm=2793b638e1c5fad519ace74ebb41c6115772cf3dfd0d5c5a0141294b3e0774b6&)
thanks!!
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
Glad it worked!