From 37f5304cc51536e6be645e9db1476b16c45fd365 Mon Sep 17 00:00:00 2001 From: Ahmad Raza Khokhar Date: Mon, 27 Jan 2025 10:27:41 +0500 Subject: [PATCH 1/2] Create useEmailValidator.tsx --- .../playground/hooks/useEmailValidator.tsx | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 compiler/apps/playground/hooks/useEmailValidator.tsx diff --git a/compiler/apps/playground/hooks/useEmailValidator.tsx b/compiler/apps/playground/hooks/useEmailValidator.tsx new file mode 100644 index 0000000000000..35d508cff55c3 --- /dev/null +++ b/compiler/apps/playground/hooks/useEmailValidator.tsx @@ -0,0 +1,21 @@ +// Regex +const strictEmailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; + +// Hook +export const useEmailValidator = (email: string) => { + // Error State + let errorMsg = ""; + + if (!email) { + errorMsg = "Email is required field"; + } else if (!email.includes("@")) { + errorMsg = "'@' is missing from the email"; + } else if (!email.includes(".")) { + errorMsg = "'.' is missing from the email"; + } else if (!strictEmailRegex.test(email)) { + errorMsg = "Please enter a valid email"; + } + + // Final output + return errorMsg; +}; From 8f1576a873866d96365bf2477f6fef5f6456be79 Mon Sep 17 00:00:00 2001 From: Ahmad Raza Khokhar Date: Mon, 27 Jan 2025 10:34:05 +0500 Subject: [PATCH 2/2] Rename useEmailValidator.tsx to useEmailValidator.ts --- .../hooks/{useEmailValidator.tsx => useEmailValidator.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename compiler/apps/playground/hooks/{useEmailValidator.tsx => useEmailValidator.ts} (100%) diff --git a/compiler/apps/playground/hooks/useEmailValidator.tsx b/compiler/apps/playground/hooks/useEmailValidator.ts similarity index 100% rename from compiler/apps/playground/hooks/useEmailValidator.tsx rename to compiler/apps/playground/hooks/useEmailValidator.ts