diff --git a/compiler/apps/playground/hooks/useEmailValidator.ts b/compiler/apps/playground/hooks/useEmailValidator.ts new file mode 100644 index 0000000000000..35d508cff55c3 --- /dev/null +++ b/compiler/apps/playground/hooks/useEmailValidator.ts @@ -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; +};