diff --git a/components/url-input.tsx b/components/url-input.tsx
index a86f594..86ceb2f 100644
--- a/components/url-input.tsx
+++ b/components/url-input.tsx
@@ -10,11 +10,22 @@ import { useUrlStore } from "@/store/url"
function UrlInput() {
const [url, setUrl] = useState("")
const [loading, setLoading] = useState(false)
+ const [error, setError] = useState("")
+
const { addUrl } = useUrlStore()
const handleSubmit = async () => {
+ setError("")
if (!url) return
+ // validate url
+ const urlRegex = /^(ftp|http|https):\/\/[^ "]+$/
+
+ if (!urlRegex.test(url)) {
+ setError("Enter a valid url")
+ return
+ }
+
setLoading(true)
const res = await fetch("/api/url", {
@@ -58,11 +69,12 @@ function UrlInput() {
className="relative mx-auto my-12 md:w-1/2"
>
setUrl(e.target.value)}
onKeyUp={(e) => e.key === "Enter" && handleSubmit()}
disabled={loading}
+ className="pr-12"
/>
+
+ {error &&
{error}
}
)
}