shadcn.io is not affiliated with official shadcn/ui
Form examples · Basic Forms
URL Input Form for React
A form with one website input that only saves a valid URL.
People type URLs without a scheme or with typos. This form checks the website with Zod's z.url(), so example.com without https:// fails with "Please enter a valid URL." A Controller connects the Input to React Hook Form inside a Field, with a FieldDescription and a FieldError, and type="url" brings up a keyboard with / and .com on phones. Use it for profile settings, portfolio links or social links.
Preview
Installation
FAQ
What does the URL check require?
website is z.url("Please enter a valid URL."). https://example.com passes. example.com, with no scheme, fails. To allow only https, pass protocol: /^https$/ in the options.
What is type url doing on the input?
type="url" changes the mobile keyboard, and the browser also checks the format on submit: a value without a scheme gets the browser's own hint before Zod runs. Add noValidate to the form to leave the check to Zod.
Where do the hint and the error live?
FieldDescription says "Enter your website or portfolio URL." FieldError shows "Please enter a valid URL." The placeholder is https://example.com.
How is the website field registered?
The Controller has name="website" and control={form.control}, and spreads field onto the Input. defaultValues sets website to "", which fails z.url() on submit. FieldLabel htmlFor matches the input's id, url-input-form-website.
Related
- Email Input FormA newsletter form with one email input
- Simple Text Input FormA form with one username text input that
- Phone Input FormA phone number form that checks the
- URL InputA website field with a link icon and a
- Profile Edit FormA profile form prefilled with a username
- Simple Input with Label FieldAn email input with its label above it