Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Modified email validation directive to have a more restrictive regex #1813

Merged
merged 5 commits into from
Jul 12, 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
13 changes: 12 additions & 1 deletion src/modules/email-validation/email-validation.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ describe('Email validation', () => {
expect(ngModel.control.pristine).toBe(false);
expect(ngModel.control.touched).toBe(false);
}));

it('should validate incorrect input — multiple @ symbols in email', fakeAsync(() => {
fixture.detectChanges();
tick();
setInput(nativeElement, 'joe@abc.com@abc.com', fixture);
fixture.detectChanges();

expect(nativeElement.querySelector('input').value).toBe('joe@abc.com@abc.com');

expect(ngModel.control.valid).toBe(false);
expect(ngModel.control.pristine).toBe(false);
expect(ngModel.control.touched).toBe(false);
}));
it('should handle invalid and then valid input', fakeAsync(() => {
fixture.detectChanges();
tick();
Expand Down
4 changes: 3 additions & 1 deletion src/modules/email-validation/email-validation.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export class SkyEmailValidationDirective implements Validator {
}

public emailIsValid(email: string): boolean {
let regex = /[\w\-]+@([\w\-]+\.)+[\w\-]+/;
// The regex was obtained from http://emailregex.com/ — which claims to correctly handle ~99% of all email addresses.
// tslint:disable-next-line:max-line-length
let regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regex.test(email);
}
}