Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5e3e5a7

Browse files
Merge pull request TheAlgorithms#501 from baciucristian/email-validation
Modify ValidateEmail.js to accept all suffixes not just '.com'
2 parents 879b8ad + a57323b commit 5e3e5a7

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

‎String/ValidateEmail.js‎

Lines changed: 2 additions & 4 deletions
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.: mahfoudh.arous@gmail.com -> true
5+
e.g.: mahfoudh.arous@helsinki.edu -> 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‎ renamed to ‎String/test/ValidateEmail.test.js‎

Lines changed: 5 additions & 1 deletion
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('mahfoudh.arous@gmail.com')).toEqual(true)
1414
})
1515

16+
it('expects to return true', () => {
17+
expect(validateEmail('icristianbaciu@.helsinki.edu')).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)

AltStyle によって変換されたページ (->オリジナル) /