Skip to content

Modify ValidateEmail.js to accept all suffixes not just '.com' #501

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

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
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
6 changes: 2 additions & 4 deletions String/ValidateEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
function that takes a string input and return either it is true of false
a valid email address
e.g.: [email protected] -> true
e.g.: [email protected] -> true
e.g.: mahfoudh.arous.com ->false
*/

const validateEmail = (str) => {
if (str === '' || str === null) {
throw new TypeError('Email Address String Null or Empty.')
}
if (str.startsWith('@') === true || !str.includes('@') || !str.endsWith('.com')) {
return false
}

return true
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str)
}

export { validateEmail }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validateEmail } from './ValidateEmail'
import { validateEmail } from '../ValidateEmail'

describe('Validation of an Email Address', () => {
it('expects to return false', () => {
Expand All @@ -13,6 +13,10 @@ describe('Validation of an Email Address', () => {
expect(validateEmail('[email protected]')).toEqual(true)
})

it('expects to return true', () => {
expect(validateEmail('[email protected]')).toEqual(true)
})

it('expects to throw a type error', () => {
expect(() => { validateEmail('') }).toThrow('Email Address String Null or Empty.')
})
Expand Down