I'm currently utilizing form builders for E-mail validation.
TS:
this.registerForm = this.formBuilder.group({
userName: [
'',
[
Validators.required,
Validators.email,
Validators.pattern(
'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$'
),
],
],
});
}
get f() {
return this.registerForm.controls;
}
validateEmail() {
this.submitted = true;
if (this.registerForm.invalid) {
console.log('Invalid');
return;
}
console.log(this.registerForm.value.userName + ' is a valid Email');
}
HTML
<form [formGroup]="registerForm" (ngSubmit)="validateEmail()">
<input
[ngClass]="{
'is-invalid': submitted && f.userName.errors,
blueLine: submitted && f.userName.errors
}"
type="text"
formControlName="userName"
/>
<button (click)="validateEmail()">Validate</button>
</form>
A scenario requires allowing users to input white spaces at the end of an E-mail address. How can these trailing white spaces be removed before validating the E-mail?
Example:
"someone.someone.com " // 3 white spaces after .com should be trimmed