From 0ba4d43518fe10725f27ea806bd15fe1153c4ac2 Mon Sep 17 00:00:00 2001 From: ducanh Date: Tue, 31 Mar 2026 00:19:52 +0700 Subject: [PATCH] update validate --- src/components/auth/SignUpForm.tsx | 75 ++++++++++++++++-------------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/src/components/auth/SignUpForm.tsx b/src/components/auth/SignUpForm.tsx index 134cd71..d8b156f 100644 --- a/src/components/auth/SignUpForm.tsx +++ b/src/components/auth/SignUpForm.tsx @@ -23,10 +23,9 @@ export default function SignUpForm() { const [errorMsg, setErrorMsg] = useState(""); const [loading, setLoading] = useState(false); - // Hàm handle thay đổi input const handleChange = (e: React.ChangeEvent) => { setFormData({ ...formData, [e.target.name]: e.target.value }); - setErrorMsg(""); // Xoá lỗi khi user gõ lại + setErrorMsg(""); }; // Hàm validate email @@ -35,12 +34,18 @@ export default function SignUpForm() { return emailRegex.test(email); }; + // Hàm validate mật khẩu mới + const isValidPassword = (pass: string) => { + // Tối thiểu 8 ký tự, 1 chữ in hoa, 1 chữ số, 1 ký tự đặc biệt + const passwordRegex = /^(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$/; + return passwordRegex.test(pass); + }; + // Xử lý khi bấm nút Sign Up (Step 1) const handleSignUpClick = async (e: React.FormEvent) => { e.preventDefault(); setErrorMsg(""); - // Validate cơ bản if (!formData.fname || !formData.lname || !formData.email || !formData.password) { setErrorMsg("Vui lòng điền đầy đủ thông tin."); return; @@ -49,12 +54,14 @@ export default function SignUpForm() { setErrorMsg("Email không đúng định dạng."); return; } + if (!isValidPassword(formData.password)) { + setErrorMsg("Mật khẩu chưa đủ điều kiện. Vui lòng nhập tối thiểu 8 ký tự, 1 in hoa, 1 số và 1 ký tự đặc biệt."); + return; + } try { setLoading(true); - // Gọi hàm CREATEOTP await apiCreateOTP(formData.email); - // Nếu thành công, chuyển sang màn hình OTP setStep(2); } catch (error) { setErrorMsg("Lỗi khi tạo OTP. Vui lòng thử lại."); @@ -91,7 +98,8 @@ export default function SignUpForm() { }; const signupRes = await apiSignUp(signupPayload); - + console.log("API Sign Up Response:", signupRes); + console.log("Đăng ký thành công!", signupRes); alert("Đăng ký thành công! Đang chuyển hướng..."); @@ -139,15 +147,12 @@ export default function SignUpForm() { {/* ----- STEP 1: FORM SIGN UP ----- */} {step === 1 && ( <> - {/* Các nút đăng ký Social (Bỏ qua xử lý logic trong ví dụ này) */} + {/* Các nút đăng ký Social */}
- {/* ... (Giữ nguyên các nút Sign up with Google / X của bạn) ... */}
@@ -209,41 +214,41 @@ export default function SignUpForm() { {/* Password */}
- -
+ + + {/* Thêm style báo đỏ ô nhập nếu pass chưa hợp lệ */} +
0 && !isValidPassword(formData.password) ? 'border border-red-500 ring-1 ring-red-500 rounded-lg' : ''}`}> - setShowPassword(!showPassword)} - className="absolute z-30 -translate-y-1/2 cursor-pointer right-4 top-1/2" - > - {showPassword ? ( - - - - ) : ( - - - - )} + setShowPassword(!showPassword)} className="absolute z-30 -translate-y-1/2 cursor-pointer right-4 top-1/2"> + {showPassword ? : }
+ + {/* Gợi ý trực quan cho người dùng */} +

+ Mật khẩu phải chứa tối thiểu 8 ký tự, 1 chữ cái in hoa, 1 chữ số và 1 ký tự đặc biệt. +

{/* Checkbox */}
- +
0 && !isValidPassword(formData.password) ? "opacity-50 cursor-not-allowed" : ""}> + { + if (isValidPassword(formData.password)) setIsChecked(val); + }} + disabled={!isValidPassword(formData.password)} + /> +

By creating an account means you agree to the{" "} @@ -260,9 +265,9 @@ export default function SignUpForm() {