I am trying to validate input using hooks but facing a problem as I want that if users leave the email blank and go to password then I want to show a message that email can't be empty also want to check using a regex that email is valid or not if now show email invalid but I am not able to see the email error message in the return. Any help would be appreciated.
I am pretty new to react hooks.
export default function SignInForm() {
const [email, setEmail] = useState(null);
const [password, setlPassword] = useState(null);
const [emailError, setEmailError] = useState(null);
const emailValidation = (e) => {
setEmail(e.target.value)
if(!email){
setEmailError('Enter a Email Address')
}
}
console.log(emailError,"emailerror")
const { t } = useTranslation();
return (
<Form fontColor={(props) => props.theme.colors.grey.base}>
<label>{t('E-mail')}</label>
<Input
id={'email'}
type={'email'}
onChange={emailValidation}
/>
<Status>{emailError}</Status>
<label>{t('Password')}</label>
<Input
id={'password'}
type={'password'}
onChange={(e) => setlPassword(e.target.value)}
/>
<Column>
<ForgetText>{t('Forgotten')}</ForgetText>
</Column>
<Button themeBlue width={'336px'} onClick={() => onSubmit()}>
{t('Sign In')}
</Button>
</Forms>