Skip to content

Commit 241e1fb

Browse files
committed
Add custom validator for phone number lengths
1 parent 544066f commit 241e1fb

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/app/core/components/two-factor-auth/two-factor-auth.component.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import {
33
UntypedFormBuilder,
4+
UntypedFormControl,
45
UntypedFormGroup,
56
Validators,
67
} from '@angular/forms';
@@ -50,6 +51,7 @@ export class TwoFactorAuthComponent implements OnInit {
5051
async ngOnInit() {
5152
this.loading = true;
5253
this.methods = await this.api.idpuser.getTwoFactorMethods();
54+
5355
this.loading = false;
5456
}
5557

@@ -140,13 +142,22 @@ export class TwoFactorAuthComponent implements OnInit {
140142
} else if (this.method === 'sms') {
141143
contactInfoControl.setValidators([
142144
Validators.required,
143-
Validators.maxLength(18),
144-
Validators.minLength(17),
145+
this.smsLengthValidator(),
145146
]);
146147
}
147148
contactInfoControl.updateValueAndValidity();
148149
}
149150

151+
private smsLengthValidator() {
152+
return (control: UntypedFormControl) => {
153+
const value = control.value || '';
154+
if (value.length === 14 || value.length === 18 || value.length === 17) {
155+
return null;
156+
}
157+
return { invalidSmsLength: true };
158+
};
159+
}
160+
150161
hasMethod(method: string): boolean {
151162
return this.methods.some((m) => m.method === method);
152163
}

0 commit comments

Comments
 (0)