"use client" import * as React from "react" import { zodResolver } from "@hookform/resolvers/zod" import { CheckIcon } from "lucide-react" import { Controller, useForm, useWatch } from "react-hook-form" import { toast } from "sonner" import * as z from "zod" import { Button } from "@/registry/new-york-v4/ui/button" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/registry/new-york-v4/ui/card" import { Field, FieldError, FieldGroup, FieldLabel, } from "@/registry/new-york-v4/ui/field" import { InputGroup, InputGroupAddon, InputGroupInput, } from "@/registry/new-york-v4/ui/input-group" import { Progress } from "@/registry/new-york-v4/ui/progress" const passwordRequirements = [ { id: "length", label: "At least 8 characters", test: (val: string) => val.length >= 8, }, { id: "lowercase", label: "One lowercase letter", test: (val: string) => /[a-z]/.test(val), }, { id: "uppercase", label: "One uppercase letter", test: (val: string) => /[A-Z]/.test(val), }, { id: "number", label: "One number", test: (val: string) => /\d/.test(val) }, { id: "special", label: "One special character", test: (val: string) => /[!@#$%^&*(),.?":{}|<>]/.test(val), }, ] const formSchema = z.object({ password: z .string() .min(8, "Password must be at least 8 characters") .refine( (val) => /[a-z]/.test(val), "Password must contain at least one lowercase letter" ) .refine( (val) => /[A-Z]/.test(val), "Password must contain at least one uppercase letter" ) .refine( (val) => /\d/.test(val), "Password must contain at least one number" ) .refine( (val) => /[!@#$%^&*(),.?":{}|<>]/.test(val), "Password must contain at least one special character" ), }) export default function FormRhfPassword() { const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { password: "", }, }) const password = useWatch({ control: form.control, name: "password", }) // Calculate password strength. const metRequirements = passwordRequirements.filter((req) => req.test(password || "") ) const strengthPercentage = (metRequirements.length / passwordRequirements.length) * 100 // Determine strength level and color. const getStrengthColor = () => { if (strengthPercentage === 0) return "bg-neutral-200" if (strengthPercentage <= 40) return "bg-red-500" if (strengthPercentage <= 80) return "bg-yellow-500" return "bg-green-500" } const allRequirementsMet = metRequirements.length === passwordRequirements.length function onSubmit(data: z.infer) { toast("You submitted the following values:", { description: (
          {JSON.stringify(data, null, 2)}
        
), position: "bottom-right", classNames: { content: "flex flex-col gap-2", }, style: { "--border-radius": "calc(var(--radius) + 4px)", } as React.CSSProperties, }) } return ( Create Password Choose a strong password to secure your account.
( Password {/* Password strength meter. */}
{/* Requirements list. */}
{passwordRequirements.map((requirement) => { const isMet = requirement.test(password || "") return (
{requirement.label}
) })}
{fieldState.invalid && ( )}
)} />
) }