Commit acf964d3 authored by Sudheer's avatar Sudheer Committed by Saturnino Abril

Add email regex for validation (#558)

* Add email regex for validation

* Fix review comment
parent fd81cade
......@@ -107,3 +107,13 @@ export function generateId() {
return id;
}
export function isEmail(email) {
// writing a regex to match all valid email addresses is really, really hard. (see http://stackoverflow.com/a/201378)
// this regex ensures:
// - at least one character that is not a space, comma, or @ symbol
// - followed by a single @ symbol
// - followed by at least one character that is not a space, comma, or @ symbol
// this prevents <Outlook Style> outlook.style@domain.com addresses and multiple comma-separated addresses from being accepted
return (/^[^ ,@]+@[^ ,@]+$/).test(email);
}
......@@ -3,7 +3,7 @@
import assert from 'assert';
import {isMinimumServerVersion} from 'utils/helpers';
import {isMinimumServerVersion, isEmail} from 'utils/helpers';
describe('Helpers', () => {
it('isMinimumServerVersion', () => {
......@@ -29,3 +29,80 @@ describe('Helpers', () => {
assert.ok(!isMinimumServerVersion());
});
});
describe('Utils.isEmail', function() {
it('', function() {
for (const data of [
{
email: 'prettyandsimple@example.com',
valid: true,
},
{
email: 'very.common@example.com',
valid: true,
},
{
email: 'disposable.style.email.with+symbol@example.com',
valid: true,
},
{
email: 'other.email-with-dash@example.com',
valid: true,
},
{
email: 'fully-qualified-domain@example.com',
valid: true,
},
{
email: 'user.name+tag+sorting@example.com',
valid: true,
},
{
email: 'x@example.com',
valid: true,
},
{
email: 'example-indeed@strange-example.com',
valid: true,
},
{
email: 'admin@mailserver1',
valid: true,
},
{
email: '#!$%&\'*+-/=?^_`{}|~@example.org',
valid: true,
},
{
email: 'example@s.solutions',
valid: true,
},
{
email: 'Abc.example.com',
valid: false,
},
{
email: 'A@b@c@example.com',
valid: false,
},
{
email: '<testing> test.email@example.com',
valid: false,
},
{
email: 'test <test@address.do>',
valid: false,
},
{
email: 'comma@domain.com, separated@domain.com',
valid: false,
},
{
email: 'comma@domain.com,separated@domain.com',
valid: false,
},
]) {
assert.equal(isEmail(data.email), data.valid);
}
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment