Skip to content

Commit 4788963

Browse files
committed
Completed validation for phone number in contact form
1 parent 84bd12f commit 4788963

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

src/app/shared/ui-components/input/input.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
[appNoSpace]="noSpace"
2222
autocomplete="off"
2323
(blur)="blur()" />
24-
<mat-error *ngIf="errorMessage">{{errorMessage}}</mat-error>
2524
<mat-error *ngIf="control.touched && control.invalid">
2625
{{
2726
(control.errors.required) ? "Please enter "+name.toLowerCase() :
2827
(control.errors.minlength) ? name + " must be greater than " + (control.errors.minlength.requiredLength-1) :
2928
(control.errors.maxlength) ? name + " must be lesser than " + control.errors.maxlength.requiredLength :
30-
'Please enter a valid '+name
29+
(control.errors.incorrect) ? control.errors.incorrect : ''
3130
}}
3231
</mat-error>
3332
</mat-form-field>
33+
errorMessage: {{control.errors | json}}
3434
</div>
3535
</ng-container>
3636

src/app/shared/ui-components/input/input.component.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class InputComponent implements OnInit {
2525
@Input() label = '';
2626
@Input() hasHint = false;
2727
@Input() hintMessage = '';
28-
@Input() errorMessage = '';
28+
@Input() errorMessage: string | boolean = null;
2929
@Input() extraClass: string | string[] = '';
3030

3131
// directives
@@ -43,12 +43,6 @@ export class InputComponent implements OnInit {
4343
// tslint:disable-next-line: no-output-on-prefix
4444
@Output() onBlur: EventEmitter<boolean>;
4545

46-
ngOnChanges(change) {
47-
if (change.errorMessage) {
48-
this.ref.detectChanges();
49-
}
50-
}
51-
5246
ngOnInit() { }
5347

5448
blur() {

src/app/shared/ui-components/mobile-number/mobile-number.component.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { ChangeDetectorRef, Component, Input, OnInit } from '@angular/core';
22
import { FormControl, FormGroup } from '@angular/forms';
33
import { UtilityFunctions } from 'src/app/utilities/app.utility';
44
// Get an instance of `PhoneNumberUtil`.
@@ -17,11 +17,12 @@ export class MobileNumberComponent implements OnInit {
1717
countryCodeSearchVal: FormControl;
1818
countryCodes: Array<any> = [];
1919
filterCountryCode: Array<any>;
20-
errorMessage: string;
20+
errorMessage: string | boolean;
2121
selectedCodeName: any;
2222

2323
constructor(
24-
private util: UtilityFunctions
24+
private util: UtilityFunctions,
25+
private ref: ChangeDetectorRef
2526
) {
2627
// Get the country code list
2728
const reg = phoneUtil.getSupportedRegions();
@@ -59,15 +60,17 @@ export class MobileNumberComponent implements OnInit {
5960
if (codeName) {
6061
this.selectedCodeName = codeName;
6162
}
62-
if (this.mobileNumber.valid && this.mobileNumber.value && this.selectedCodeName) {
63+
if ( (this.mobileNumber.valid || this.mobileNumber.errors.incorrect)
64+
&& this.mobileNumber.value && this.selectedCodeName) {
6365
const phoneCheck = phoneUtil.parseAndKeepRawInput(this.mobileNumber.value, this.selectedCodeName);
6466
if (!phoneUtil.isValidNumber(phoneCheck)) {
65-
this.errorMessage = 'Please enter a valid phone number';
67+
/* If phone invalid set error */
68+
this.mobileNumber.setErrors({incorrect: 'Please enter a valid phone number'});
69+
this.mobileNumber.markAsTouched();
6670
} else {
67-
this.errorMessage = null;
71+
/* If phone valid remove the error */
72+
this.mobileNumber.setErrors(null);
6873
}
69-
} else {
70-
this.errorMessage = null;
7174
}
7275
}
7376

0 commit comments

Comments
 (0)