import { useState } from
"react"
;
import { FormControl, FormLabel, FormErrorMessage,
FormHelperText, Input, Button } from
"@chakra-ui/react"
;
function
App() {
const [passwordLength, setPasswordLength] = useState(
""
);
const handlePasswordChange = (e) => {
setPasswordLength(e.target.value);
}
const error = passwordLength.length < 8
return
(
<div className=
"flex flex-col justify-center
items-center my-44"
>
<h1 className=
"text-green-500 font-bold text-lg"
>
GeeksforGeeks
</h1>
<h2 className=
"font-semibold mt-2"
>
ReactJS Chakra UI Form Validation
</h2>
<FormControl className=
"shadow-lg p-8 rounded-lg mt-5"
isRequired isInvalid={error}>
<FormLabel>Name</FormLabel>
<Input className=
"border border-gray-200
rounded-lg p-1 w-96"
type=
"text"
placeholder=
"Your name..."
/>
<FormLabel className=
"mt-5"
>Password</FormLabel>
<Input className=
"border border-gray-200
rounded-lg p-1 w-96"
value={passwordLength}
onChange={handlePasswordChange}
type=
"password"
placeholder=
"Your password..."
/>
{error ? (
<FormErrorMessage>
Password is less than 8 characters...
</FormErrorMessage>
) : (
<FormHelperText>
You are good to go...
</FormHelperText>
)}
<Button className=
"bg-blue-600 text-white
p-1.5 mx-5 rounded-lg mt-5"
type=
"submit"
>Submit</Button>
</FormControl>
</div>
);
}
export
default
App;