From 94ff03a7e1901a1cca51cc9163ea49f2e91058d6 Mon Sep 17 00:00:00 2001
From: Jeremie BRISON <ext.sopra.jbrison@grandlyon.com>
Date: Mon, 21 Dec 2020 10:21:17 +0100
Subject: [PATCH] fix(dev) : update regex phone + add space every 2 numbers

---
 src/app/form/form.component.html |  2 +-
 src/app/form/form.component.ts   | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/app/form/form.component.html b/src/app/form/form.component.html
index 0b116891d..7d4eae895 100644
--- a/src/app/form/form.component.html
+++ b/src/app/form/form.component.html
@@ -55,7 +55,7 @@
   </div>
   <p>VOUS JOINDRE</p>
   <p>Téléphone*</p>
-  <input type="text" formControlName="contactPhone" />
+  <input type="text" formControlName="contactPhone" (input)="modifyPhoneInput($event.target.value)" />
   <app-validator-form [control]="getStructureControl('contactPhone')"></app-validator-form>
   <p>Courriel*</p>
   <input type="email" formControlName="contactMail" />
diff --git a/src/app/form/form.component.ts b/src/app/form/form.component.ts
index 733bd94d2..85485bd4a 100644
--- a/src/app/form/form.component.ts
+++ b/src/app/form/form.component.ts
@@ -87,7 +87,10 @@ export class FormComponent implements OnInit {
         street: new FormControl(structure.address.street, Validators.required),
         commune: new FormControl(structure.address.commune, Validators.required),
       }),
-      contactPhone: new FormControl(structure.contactPhone, [Validators.required, Validators.pattern('[0-9]{10}')]),
+      contactPhone: new FormControl(structure.contactPhone, [
+        Validators.required,
+        Validators.pattern('([0-9]{2} ){4}[0-9]{2}'),
+      ]),
       contactMail: new FormControl(structure.contactMail, [
         Validators.required,
         Validators.pattern('[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,3}$'),
@@ -149,6 +152,16 @@ export class FormComponent implements OnInit {
     return this.structureForm.get('address').get(nameControl);
   }
 
+  public modifyPhoneInput(phoneNumber: string): void {
+    // Take length of phone number without spaces.
+    let phoneNoSpace = phoneNumber.replace(/\s/g, '');
+    // Check to refresh every 2 number.
+    if (phoneNoSpace.length % 2 == 0) {
+      // Add space every 2 number
+      this.structureForm.get('contactPhone').setValue(phoneNoSpace.replace(/(?!^)(?=(?:\d{2})+$)/g, ' '));
+    }
+  }
+
   public getTime(day: string): FormArray {
     return this.structureForm.get('hours').get(day).get('time') as FormArray;
   }
-- 
GitLab