So I’m making a sign up form for a website that uses react and tailwindcss. For email field, I wanna see if the email is valid or not. So i used the native tailwind peer-{modifier} for it.
Code:
<div className='flex w-full flex-col space-y-2'>
<label htmlFor='email' className='text-sm text-gray-600'>
Email address
</label>
<input
type='email'
id='email'
autoComplete='email'
required
className='peer relative block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm disabled:bg-slate-50 disabled:text-slate-500 disabled:border-slate-200 disabled:shadow-none
invalid:border-pink-500 invalid:text-pink-600
focus:invalid:border-pink-500 focus:invalid:ring-pink-500'
value={email}
onChange={e => setEmail(e.target.value)}
/>
<p className="mt-2 invisible peer-invalid:visible text-pink-600 text-sm">
Please provide a valid email address.
</p>
</div>
Screenshot:
it shows this error message even when there’s nothing entered yet. How do i fix it?
2
Answers
Try to use
$v.user.email.$error
condition to avoid the error that is showing even if the fields are empty at starting.Source: https://stackoverflow.com/a/65151936/13680835
The reason it shows at the start is because of
required
attribute. Whenrequired
is set in a field, it means that this field must have some values. So at the start, your input field is in an invalid state -> the error message appears.Here are some solutions
input
field)input
field)In short, I use
peer-placeholder-shown
to hide the error message, but the specificity ofpeer-placeholder-shown
is lower than that of ‘peer-invalid’ so I have to use the question mark to push its specificity. Here is the Tailwind demo if you need.Hope it helps.