Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions compiler/apps/playground/hooks/useEmailValidator.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Loading