Skip to content

Commit a57323b

Browse files
committed
Modify ValidateEmail.js to accept all suffixes
1 parent be96654 commit a57323b

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

String/ValidateEmail.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
function that takes a string input and return either it is true of false
33
a valid email address
44
e.g.: [email protected] -> true
5+
e.g.: [email protected] -> true
56
e.g.: mahfoudh.arous.com ->false
67
*/
78

89
const validateEmail = (str) => {
910
if (str === '' || str === null) {
1011
throw new TypeError('Email Address String Null or Empty.')
1112
}
12-
if (str.startsWith('@') === true || !str.includes('@') || !str.endsWith('.com')) {
13-
return false
14-
}
1513

16-
return true
14+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str)
1715
}
1816

1917
export { validateEmail }

String/ValidateEmail.test.js String/test/ValidateEmail.test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { validateEmail } from './ValidateEmail'
1+
import { validateEmail } from '../ValidateEmail'
22

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

16+
it('expects to return true', () => {
17+
expect(validateEmail('[email protected]')).toEqual(true)
18+
})
19+
1620
it('expects to throw a type error', () => {
1721
expect(() => { validateEmail('') }).toThrow('Email Address String Null or Empty.')
1822
})

0 commit comments

Comments
 (0)