Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Backport of Add email validation to core utility #33699

Merged
merged 1 commit into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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