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