diff --git a/src/Pages/Book/book.js b/src/Pages/Book/book.js index 1566ee3..2eed919 100644 --- a/src/Pages/Book/book.js +++ b/src/Pages/Book/book.js @@ -9,170 +9,224 @@ const Book = () => { passType: "", }); + const [errors, setErrors] = useState({}); + const [submitted, setSubmitted] = useState(false); + + // Shared input style + const inputStyle = { + display: "block", + width: "100%", + padding: "10px", + margin: "8px 0 4px 0", + backgroundColor: "black", + color: "white", + border: "1px solid #ffffff33", + borderRadius: "6px", + fontSize: "15px", + }; + + const errorText = { + color: "#ff5555", + fontSize: "13px", + marginBottom: "10px", + }; + + /* Handle input updates */ const handleChange = (e) => { const { name, value } = e.target; - setFormData((prevData) => ({ - ...prevData, - [name]: value, + + const cleanValue = + name === "phone" || name === "email" + ? value.trim().replace(/\s+/g, "") + : value; + + setFormData((prev) => ({ + ...prev, + [name]: cleanValue, })); + + validateField(name, cleanValue); }; + /*Validation Logic */ + const validateField = (name, value) => { + let msg = ""; + + if (name === "phone") { + // 🌍 E.164 format + const phoneRegex = /^\+?[1-9]\d{1,14}$/; + if (!value) msg = "Phone number is required."; + else if (!phoneRegex.test(value)) + msg = + "Enter a valid international number (e.g. +14155552671 or +447700900123)."; + } + + if (name === "email") { + // valid emails only + const emailRegex = + /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[A-Za-z]{2,}$/; + if (!value) msg = "Email address is required."; + else if (!emailRegex.test(value)) + msg = "Enter a valid email address (e.g. user@example.com)."; + } + + setErrors((prev) => ({ ...prev, [name]: msg })); + }; + + /* Handle Form Submit */ const handleSubmit = (e) => { e.preventDefault(); - console.log("Form Submitted:", formData); + + Object.keys(formData).forEach((key) => + validateField(key, formData[key]) + ); + + if (Object.values(errors).some((err) => err)) { + alert("Please fix the highlighted errors before submitting."); + return; + } + + console.log("✅ Form Submitted:", formData); + setSubmitted(true); + alert("🎉 Booking submitted successfully!"); }; + const isInvalid = + errors.phone || errors.email || !formData.phone || !formData.email; + return (
-
- + Book Your Workspace +
+
- - - - - - - - - + {/* Name */} + + + + {/* Phone */} + + + {errors.phone &&

{errors.phone}

} + + {/* Email */} + + + {errors.email &&

{errors.email}

} + + {/* Company */} + + + + {/* Pass Type */} + + + {/* Submit */}