mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
Add Email Validation Function (#462)
* Add Email Validation Function * fix standard styling issues
This commit is contained in:
19
String/ValidateEmail.js
Normal file
19
String/ValidateEmail.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
function that takes a string input and return either it is true of false
|
||||||
|
a valid email address
|
||||||
|
e.g.: mahfoudh.arous@gmail.com -> 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
|
||||||
|
}
|
||||||
|
|
||||||
|
export { validateEmail }
|
19
String/ValidateEmail.test.js
Normal file
19
String/ValidateEmail.test.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { validateEmail } from './ValidateEmail'
|
||||||
|
|
||||||
|
describe('Validation of an Email Address', () => {
|
||||||
|
it('expects to return false', () => {
|
||||||
|
expect(validateEmail('mahfoudh.arous.com')).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return false', () => {
|
||||||
|
expect(validateEmail('mahfoudh.arous@com')).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return true', () => {
|
||||||
|
expect(validateEmail('mahfoudh.arous@gmail.com')).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to throw a type error', () => {
|
||||||
|
expect(() => { validateEmail('') }).toThrow('Email Address String Null or Empty.')
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user