Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2.  
  3. import * as moment from 'moment';
  4. import 'moment/locale/pt-br';
  5.  
  6. import {
  7.   FormGroup,
  8.   FormControl,
  9.   FormBuilder,
  10.   Validators,
  11. } from '@angular/forms';
  12.  
  13.  
  14. @Injectable()
  15. export class FormValidationsService {
  16.  
  17.   checkInvalidTouched(form: FormGroup, field: string) {
  18.     return (
  19.       !form.get(field).valid &&
  20.       (form.get(field).touched || form.get(field).dirty)
  21.     );
  22.   }
  23.  
  24.   //Função praa a validação de data
  25.   isDateValid(form: FormGroup, field: string) {
  26.     let date = form.get(field).value;
  27.     if (date != null) {
  28.       let dateOfBirth = moment(date, "DD-MM-YYYY","pt-br");
  29.       if(moment(dateOfBirth).isValid() && date.length === 10) {
  30.         if(moment(dateOfBirth).isBefore(moment.now()))
  31.             return true;    
  32.           else
  33.             return false;
  34.       }
  35.       else {
  36.         return false;
  37.       }
  38.     }
  39.   }
  40.  
  41.     //Função para a validação do Email
  42.   isEmailValid(form: FormGroup, field: string): string {
  43.     if(form.get(field).value == "" &&  !form.get(field).valid) {
  44.       return "empty";
  45.     }
  46.     else if(!form.get(field).valid && form.get(field).value != "" && form.get(field).dirty) {
  47.       return "invalid";
  48.     }
  49.   }
  50.   //Função para a validação do Telefone
  51.   isPhoneValid(form: FormGroup, field: string) {
  52.     let phone = form.get(field).value;
  53.     if (phone != null) {
  54.       if (phone.length >= 10)
  55.         return true;
  56.       else
  57.       return false;
  58.     }
  59.   }
  60.  
  61.   //Função para a validação do CPF
  62.   isCPFValid(form: FormGroup, field: string) : boolean
  63.   {
  64.     let cpf = form.get(field).value;
  65.     if (cpf != null) {
  66.       let numeros, digitos, soma, i, resultado, digitos_iguais;
  67.     digitos_iguais = 1;
  68.     if (cpf.length < 11)
  69.           return false;
  70.     for (i = 0; i < cpf.length - 1; i++)
  71.           if (cpf.charAt(i) != cpf.charAt(i + 1))
  72.                 {
  73.                 digitos_iguais = 0;
  74.                 break;
  75.                 }
  76.     if (!digitos_iguais)
  77.           {
  78.           numeros = cpf.substring(0,9);
  79.           digitos = cpf.substring(9);
  80.           soma = 0;
  81.           for (i = 10; i > 1; i--)
  82.                 soma += numeros.charAt(10 - i) * i;
  83.           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  84.           if (resultado != digitos.charAt(0))
  85.                 return false;
  86.           numeros = cpf.substring(0,10);
  87.           soma = 0;
  88.           for (i = 11; i > 1; i--)
  89.                 soma += numeros.charAt(11 - i) * i;
  90.           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  91.           if (resultado != digitos.charAt(1))
  92.                 return false;
  93.           return true;
  94.           }
  95.     else
  96.         return false;
  97.     }
  98.  
  99.   }
  100.  
  101.  
  102.   applyErrorCss(form: FormGroup, field: string) {
  103.     return {
  104.       'form__field--error': this.checkInvalidTouched(form, field),
  105.     };
  106.   }
  107.  
  108.   validateAllFormFields(formGroup: FormGroup) {
  109.     Object.keys(formGroup.controls).forEach(field => {
  110.       const control = formGroup.get(field);
  111.       if (control instanceof FormControl) {
  112.         control.markAsTouched({ onlySelf: true });
  113.       } else if (control instanceof FormGroup) {
  114.         this.validateAllFormFields(control);
  115.       }
  116.     });
  117.   }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement