conatct validation
This commit is contained in:
@@ -9,13 +9,36 @@ import { MessageSquare } from "lucide-react";
|
||||
import { useLanguage } from "@/contexts/language-context";
|
||||
import axios from "axios";
|
||||
|
||||
function validateContact(input: string) {
|
||||
const value = input.trim();
|
||||
|
||||
// Email regex (simple and reliable)
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
|
||||
// Uzbek phone formats:
|
||||
// - 9-digit numbers: 937665544
|
||||
// - +998XXXXXXXXX
|
||||
const phoneRegex = /^(\+998\d{9}|\d{9})$/;
|
||||
|
||||
if (emailRegex.test(value)) {
|
||||
return { valid: true, type: "email" };
|
||||
}
|
||||
|
||||
if (phoneRegex.test(value)) {
|
||||
return { valid: true, type: "phone" };
|
||||
}
|
||||
|
||||
return { valid: false, message: "Enter valid email or phone number" };
|
||||
}
|
||||
|
||||
export default function Contact() {
|
||||
const [formData, setFormData] = useState({
|
||||
name: "",
|
||||
email: "",
|
||||
connect_link: "",
|
||||
message: "",
|
||||
});
|
||||
const { t } = useLanguage();
|
||||
const [error, setError] = useState<string>("");
|
||||
|
||||
const handleChange = (
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
|
||||
@@ -29,6 +52,11 @@ export default function Contact() {
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
// Handle form submission
|
||||
const valid = validateContact(formData.connect_link);
|
||||
if (!valid.valid) {
|
||||
setError(valid.message || "");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const token = "7940057045:AAHRFPvgUCo_7pqpXD6uq4li7-_DYx2J96g"; // Use environment variable
|
||||
const chatId = 6134458285;
|
||||
@@ -40,7 +68,7 @@ export default function Contact() {
|
||||
const message = `
|
||||
📥 Yangi xabar keldi!
|
||||
👤 Ism: ${formData.name}
|
||||
📧 Email: ${formData.email}
|
||||
📧 Bog'lanish uchun: ${formData.connect_link}
|
||||
💬 Xabar: ${formData.message}
|
||||
`;
|
||||
|
||||
@@ -52,9 +80,10 @@ export default function Contact() {
|
||||
alert("✅ Tez orada bog'lanamiz!");
|
||||
setFormData({
|
||||
name: "",
|
||||
email: "",
|
||||
connect_link: "",
|
||||
message: "",
|
||||
});
|
||||
setError('')
|
||||
} catch (error) {
|
||||
console.error("Yuborishda xatolik:", error);
|
||||
alert("❌ Yuborishda xatolik yuz berdi!");
|
||||
@@ -96,14 +125,15 @@ export default function Contact() {
|
||||
{t.contact.fields.email}
|
||||
</label>
|
||||
<Input
|
||||
type="email"
|
||||
name="email"
|
||||
value={formData.email}
|
||||
type="text"
|
||||
name="connect_link"
|
||||
value={formData.connect_link}
|
||||
onChange={handleChange}
|
||||
placeholder={t.contact.fields.email_placeholder}
|
||||
className="bg-background border-border"
|
||||
required
|
||||
/>
|
||||
{error && <p className="text-red-500 text-sm mt-1">{error}</p>}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user