Validation with a clear return path
Native email validation, linked feedback and focus on the field that needs attention.
Keyboard
- Tab reaches the email field and submit action.
- Enter submits; invalid input keeps its value and receives focus.
States covered
- Empty required field
- Invalid email
- Saved feedback
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-input-validation.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useRef,useState} from 'react';
import {Button,Field,Input} from '@bld8/ui';
export default function ContactExample(){
const [email,setEmail]=useState(''),[error,setError]=useState(''),[status,setStatus]=useState('');
const input=useRef<HTMLInputElement>(null);
return <form noValidate onSubmit={event=>{
event.preventDefault();
if(!input.current?.validity.valid){setError('Enter a valid contact email.');input.current?.focus();return;}
setStatus('Contact details saved for this example.');
}}>
<Field label="Contact email" required hint="Use an address where you can receive replies." error={error}>
<Input ref={input} type="email" name="contactEmail" value={email} onChange={event=>{setEmail(event.target.value);setError('');setStatus('');}}/>
</Field>
<Button type="submit">Save contact example</Button><p role="status">{status}</p>
</form>;
}