Skip to content

Commit

Permalink
Merge pull request #33699 from owncloud/add-emailvalidation-to-coreut…
Browse files Browse the repository at this point in the history
…ility-stable10

[stable10] Backport of Add email validation to core utility
  • Loading branch information
Vincent Petry authored Nov 29, 2018
2 parents f0fd08e + d9da651 commit 6c01ad2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
15 changes: 15 additions & 0 deletions core/js/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,21 @@ var OC = {
return path.replace(/\\/g, '/').replace(/.*\//, '');
},

/**
* Returns true if the email regexp matches the email address else false returned
* For example if email is "abc@foo.com", it will return true.
* If email address is "abc@foo.c", then false will be returned.
*
* @param emailAddress
* @returns {boolean}
*
* @since 10.1.0
*/
validateEmail: function(emailAddress) {
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@\.]{2,}$/;
return (emailRegex.exec(emailAddress) !== null);
},

/**
* Returns the dir name of the given path.
* For example for "/abc/somefile.txt" it will return "/abc"
Expand Down
2 changes: 1 addition & 1 deletion core/js/sharedialogmailview.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
if (email.length === 0)
return true

return email.match(/^[A-Za-z0-9\._%+-]+@(?:[A-Za-z0-9-]+\.)+[a-z]{2,}$/);
return OC.validateEmail(email);
},

sendEmails: function() {
Expand Down
23 changes: 23 additions & 0 deletions core/js/tests/specs/coreSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ describe('Core base tests', function() {
expect(OC.appswebroots).toBeDefined();
});
});
describe('validateEmail', function () {
it('Returns false for search abc@foo', function () {
expect(OC.validateEmail('abc@foo')).toEqual(false);
});
it('Returns false for search abc@foo.', function () {
expect(OC.validateEmail('abc@foo.')).toEqual(false);
});
it('Returns false for search abc', function () {
expect(OC.validateEmail('abc')).toEqual(false);
});
it('Returns false for search abc@foo.a', function () {
expect(OC.validateEmail('abc@foo.a')).toEqual(false);
});
it('Returns true for search abc@foo.aa', function () {
expect(OC.validateEmail('abc@foo.aa')).toEqual(true);
});
it('Returns true for search abc@f.aaa', function () {
expect(OC.validateEmail('abc@f.aaa')).toEqual(true);
});
it('Returns true for search müller@Émile.诶西艾弗.буки', function () {
expect(OC.validateEmail('müller@Émile.诶西艾弗.буки')).toEqual(true);
});
});
describe('basename', function() {
it('Returns the nothing if no file name given', function() {
expect(OC.basename('')).toEqual('');
Expand Down
15 changes: 7 additions & 8 deletions core/js/tests/specs/sharedialogmailviewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ describe('OC.Share.ShareDialogMailView', function() {

describe('validating addresses', function() {
it('works as expected', function() {
expect(view.validateEmail('Ada.Wong@umbrella.com')[0]).toEqual('Ada.Wong@umbrella.com');
expect(view.validateEmail('Albert.Wesker@umbrella.sub-domain.com')[0]).toEqual('Albert.Wesker@umbrella.sub-domain.com');
expect(view.validateEmail('Albert_Wesker@umbrella.sub-domain.com')[0]).toEqual('Albert_Wesker@umbrella.sub-domain.com');
expect(view.validateEmail('Albert-Wesker@umbrella-new.sub-domain.com')[0]).toEqual('Albert-Wesker@umbrella-new.sub-domain.com');

expect(view.validateEmail('Jill.Valentine@umbrella..com')).toEqual(null);
expect(view.validateEmail('Jill.Valentine@um#rella.com')).toEqual(null);
expect(view.validateEmail('Jürgen.Sörensen@umbrella.com')).toEqual(null);
expect(view.validateEmail('Ada.Wong@umbrella.com')).toEqual(true);
expect(view.validateEmail('Albert.Wesker@umbrella.sub-domain.com')).toEqual(true);
expect(view.validateEmail('Albert_Wesker@umbrella.sub-domain.com')).toEqual(true);
expect(view.validateEmail('Albert-Wesker@umbrella-new.sub-domain.com')).toEqual(true);
expect(view.validateEmail('Jill.Valentine@um#rella.com')).toEqual(true);

expect(view.validateEmail('Jill.Valentine@umbrella..c')).toEqual(false);
});
});

Expand Down

0 comments on commit 6c01ad2

Please sign in to comment.