Skip to content

Commit 9b63efc

Browse files
authored
Add Email Validation Function (TheAlgorithms#462)
* Add Email Validation Function * fix standard styling issues
1 parent 135d9d1 commit 9b63efc

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

String/ValidateEmail.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
function that takes a string input and return either it is true of false
3+
a valid email address
4+
e.g.: [email protected] -> true
5+
e.g.: mahfoudh.arous.com ->false
6+
*/
7+
8+
const validateEmail = (str) => {
9+
if (str === '' || str === null) {
10+
throw new TypeError('Email Address String Null or Empty.')
11+
}
12+
if (str.startsWith('@') === true || !str.includes('@') || !str.endsWith('.com')) {
13+
return false
14+
}
15+
16+
return true
17+
}
18+
19+
export { validateEmail }

String/ValidateEmail.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { validateEmail } from './ValidateEmail'
2+
3+
describe('Validation of an Email Address', () => {
4+
it('expects to return false', () => {
5+
expect(validateEmail('mahfoudh.arous.com')).toEqual(false)
6+
})
7+
8+
it('expects to return false', () => {
9+
expect(validateEmail('mahfoudh.arous@com')).toEqual(false)
10+
})
11+
12+
it('expects to return true', () => {
13+
expect(validateEmail('[email protected]')).toEqual(true)
14+
})
15+
16+
it('expects to throw a type error', () => {
17+
expect(() => { validateEmail('') }).toThrow('Email Address String Null or Empty.')
18+
})
19+
})

0 commit comments

Comments
 (0)