Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compiler] Created useEmailValidator.tsx. #32229

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Next Next commit
Create useEmailValidator.tsx
AhmadRazaKhokhar1 authored Jan 27, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 37f5304cc51536e6be645e9db1476b16c45fd365
21 changes: 21 additions & 0 deletions compiler/apps/playground/hooks/useEmailValidator.tsx
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;
};