Created
February 28, 2024 18:36
-
-
Save lndgalante/73f3f5d6dd2effa64fc247e8c4b58551 to your computer and use it in GitHub Desktop.
Check if an email is valid or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getIsEmailValid(email: string): boolean { | |
| if (!email.includes('@') || !email.includes('.')) { | |
| return false; | |
| } | |
| const parts = email.split('@'); | |
| const hasMoreThanOneAtSymbol = parts.length !== 2; | |
| if (hasMoreThanOneAtSymbol) { | |
| return false; | |
| } | |
| const [local, domain] = parts; | |
| const isLocalIncorrect = local.length === 0; | |
| if (isLocalIncorrect) { | |
| return false; | |
| } | |
| const isDomainIncorrect = !domain.includes('.') || domain.startsWith('.') || domain.endsWith('.'); | |
| if (isDomainIncorrect) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| console.log(getIsEmailValid('test@example.com')); | |
| console.log(getIsEmailValid('test@example')); | |
| console.log(getIsEmailValid('test@.com')); | |
| console.log(getIsEmailValid('@example.com')); | |
| console.log(getIsEmailValid('test@@example.com')); | |
| console.log(getIsEmailValid('test@example.com.')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment