mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Merge pull request #501 from baciucristian/email-validation
Modify ValidateEmail.js to accept all suffixes not just '.com'
This commit is contained in:
@ -2,6 +2,7 @@
|
|||||||
function that takes a string input and return either it is true of false
|
function that takes a string input and return either it is true of false
|
||||||
a valid email address
|
a valid email address
|
||||||
e.g.: mahfoudh.arous@gmail.com -> true
|
e.g.: mahfoudh.arous@gmail.com -> true
|
||||||
|
e.g.: mahfoudh.arous@helsinki.edu -> true
|
||||||
e.g.: mahfoudh.arous.com ->false
|
e.g.: mahfoudh.arous.com ->false
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -9,11 +10,8 @@ const validateEmail = (str) => {
|
|||||||
if (str === '' || str === null) {
|
if (str === '' || str === null) {
|
||||||
throw new TypeError('Email Address String Null or Empty.')
|
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 }
|
export { validateEmail }
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { validateEmail } from './ValidateEmail'
|
import { validateEmail } from '../ValidateEmail'
|
||||||
|
|
||||||
describe('Validation of an Email Address', () => {
|
describe('Validation of an Email Address', () => {
|
||||||
it('expects to return false', () => {
|
it('expects to return false', () => {
|
||||||
@ -13,6 +13,10 @@ describe('Validation of an Email Address', () => {
|
|||||||
expect(validateEmail('mahfoudh.arous@gmail.com')).toEqual(true)
|
expect(validateEmail('mahfoudh.arous@gmail.com')).toEqual(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('expects to return true', () => {
|
||||||
|
expect(validateEmail('icristianbaciu@.helsinki.edu')).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
it('expects to throw a type error', () => {
|
it('expects to throw a type error', () => {
|
||||||
expect(() => { validateEmail('') }).toThrow('Email Address String Null or Empty.')
|
expect(() => { validateEmail('') }).toThrow('Email Address String Null or Empty.')
|
||||||
})
|
})
|
Reference in New Issue
Block a user