import {Form, Input, Select, SelectItem, Checkbox, Button} from "@heroui/react"; export default function App() { const [password, setPassword] = React.useState(""); const [submitted, setSubmitted] = React.useState(null); const [errors, setErrors] = React.useState({}); // Real-time password validation const getPasswordError = (value) => { if (value.length < 4) { return "Password must be 4 characters or more"; } if ((value.match(/[A-Z]/g) || []).length < 1) { return "Password needs at least 1 uppercase letter"; } if ((value.match(/[^a-z]/gi) || []).length < 1) { return "Password needs at least 1 symbol"; } return null; }; const onSubmit = (e) => { e.preventDefault(); const data = Object.fromEntries(new FormData(e.currentTarget)); // Custom validation checks const newErrors = {}; // Password validation const passwordError = getPasswordError(data.password); if (passwordError) { newErrors.password = passwordError; } // Username validation if (data.name === "admin") { newErrors.name = "Nice try! Choose a different username"; } if (Object.keys(newErrors).length > 0) { setErrors(newErrors); return; } if (data.terms !== "true") { setErrors({terms: "Please accept the terms"}); return; } // Clear errors and submit setErrors({}); setSubmitted(data); }; return (
setSubmitted(null)} onSubmit={onSubmit} >
{ if (validationDetails.valueMissing) { return "Please enter your name"; } return errors.name; }} label="Name" labelPlacement="outside" name="name" placeholder="Enter your name" /> { if (validationDetails.valueMissing) { return "Please enter your email"; } if (validationDetails.typeMismatch) { return "Please enter a valid email address"; } }} label="Email" labelPlacement="outside" name="email" placeholder="Enter your email" type="email" /> setErrors((prev) => ({...prev, terms: undefined}))} > I agree to the terms and conditions {errors.terms && {errors.terms}}
{submitted && (
Submitted data:
{JSON.stringify(submitted, null, 2)}
)}
); }