Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #637 from h-da/bugfix/#597-validate-password-reset…
Browse files Browse the repository at this point in the history
…-email

 #597: added email validator to reset form, show hint if the email isn…
  • Loading branch information
PatrickSkowronek authored Apr 19, 2018
2 parents 736c73d + 9c9e9a4 commit 7a0f180
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/webFrontend/src/app/auth/reset/reset.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {MatSnackBar} from '@angular/material';
import {ActivatedRoute, Router} from '@angular/router';
import {isNullOrUndefined} from 'util';
import {TitleService} from '../../shared/services/title.service';
import {emailValidator} from '../../shared/validators/validators';

@Component({
templateUrl: './reset.component.html',
Expand Down Expand Up @@ -43,6 +44,11 @@ export class ResetComponent implements OnInit {
}

requestReset() {
if (!this.resetForm.valid) {
this.snackBar.open('The email address you entered is not valid.', 'Dismiss');
return;
}

this.showProgress.toggleLoadingGlobal(true);
this.loading = true;
this.authenticationService.requestReset(this.resetForm.value.email.replace(/\s/g, '').toLowerCase())
Expand Down Expand Up @@ -80,7 +86,7 @@ export class ResetComponent implements OnInit {
this.resetForm = this.formBuilder.group({});
} else {
this.resetForm = this.formBuilder.group({
email: ['', Validators.required]
email: ['', emailValidator ]
});
}
}
Expand Down
20 changes: 19 additions & 1 deletion app/webFrontend/src/app/shared/validators/validators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FormGroup} from '@angular/forms';
import {FormControl, FormGroup} from '@angular/forms';

export function matchPasswords(passwordKey: string, confirmPasswordKey: string) {
return (group: FormGroup): { [key: string]: any } => {
Expand All @@ -12,3 +12,21 @@ export function matchPasswords(passwordKey: string, confirmPasswordKey: string)
}
};
}

/**
* A custom email validator which, in comparison to the default angular email validator,
* also checks if the email address has a top-level domain.
*
* @param {FormControl} formControl
* @returns {{emailValidator: {valid: boolean}}}
*/
export function emailValidator(formControl: FormControl) {
// Email Regex from: http://emailregex.com/
// tslint:disable-next-line:max-line-length
const EMAIL_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 EMAIL_REGEX.test(formControl.value) ? null : {
emailValidator: {
valid: false
}
};
}

0 comments on commit 7a0f180

Please sign in to comment.